kbsali / php-redmine-api

A simple PHP Redmine API client, Object Oriented

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create factory methods in all API classes

Art4 opened this issue · comments

Declaring the classes in the Redmine\Api\ namespace as final will allow us to make more future changes without worrying to much about BC.

To achieve this in a FC way we could create new factory methods create(HttpClient $client): self in all API child classes.

class ProjectApi extends AbstractApi
{
    final public static function create(HttpClient $client): self
    {
        return new self($client);
    }

    /**
     * @param Client|HttpClient $client
     */
    final public function __construct($client)
    {
        if (get_called_class() !== __CLASS__) {
            @trigger_error('Extending ' . __CLASS__ . ' is deprecated since v2.x, it will become final in v3.0.0.');
        }

        if ($client instanceof Client) {
            $this->client = $client;
        }

        // Handle HttpClient...
    }
}

Using the HttpClient in the API classes makes the AbstractApi obsolete and can be deprecated. Extending it should also trigger a deprecation.

abstract class AbstractApi 
{
    /**
     * @param Client|HttpClient $client
     */
    public function __construct($client)
    {
        @trigger_error(__CLASS__ . ' is deprecated since v2.x and will be removed in v3.0.0.');

        // old code...
    }
}

Affected classes

  1. Redmine\Api\Attachment
  2. Redmine\Api\CustomField
  3. Redmine\Api\Group
  4. Redmine\Api\Issue
  5. Redmine\Api\IssueCategory
  6. Redmine\Api\IssuePriority
  7. Redmine\Api\IssueRelation
  8. Redmine\Api\IssueStatus
  9. Redmine\Api\Membership
  10. Redmine\Api\News
  11. Redmine\Api\Project
  12. Redmine\Api\Query
  13. Redmine\Api\Role
  14. Redmine\Api\Search
  15. Redmine\Api\TimeEntry
  16. Redmine\Api\TimeEntryActivity
  17. Redmine\Api\Tracker
  18. Redmine\Api\User
  19. Redmine\Api\Version
  20. Redmine\Api\Wiki
  21. Redmine\Api\AbstractApi