Client.php 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
<?php

namespace Zendesk\API;

    /*
     * Dead simple autoloader:
     * spl_autoload_register(function($c){@include 'src/'.preg_replace('#\\\|_(?!.+\\\)#','/',$c).'.php';});
     */

/**
 * Client class, base level access
 * @package Zendesk\API
 *
 * @method Debug debug()
 * @method Tickets ticket()
 * @method Tickets tickets()
 * @method TicketFields ticketFields()
 * @method TicketForms ticketForms()
 * @method Twitter twitter()
 * @method Attachments attachments()
 * @method Requests requests()
 * @method Views views()
 * @method Users users()
 * @method UserFields userFields()
 * @method Groups groups()
 * @method GroupMemberships groupMemberships()
 * @method CustomRoles customRoles()
 * @method Forums forums()
 * @method Categories categories()
 * @method Topics topics()
 * @method Settings settings()
 * @method ActivityStream activityStream()
 * @method AuditLogs auditLogs()
 * @method Autocomplete autocomplete()
 * @method Automations automations()
 * @method JobStatuses jobStatuses()
 * @method Macros macros()
 * @method DynamicContent dynamicContent()
 * @method OAuthClients oauthClients()
 * @method OAuthTokens oauthTokens()
 * @method OrganizationFields organizationFields()
 * @method Organizations organizations()
 * @method SatisfactionRatings satisfactionRatings()
 * @method SharingAgreements sharingAgreements()
 * @method SuspendedTickets suspendedTickets()
 * @method Tags tags()
 * @method Targets targets()
 * @method Triggers triggers()
 * @method Voice voice()
 * @method Locales locales()
 * @method PushNotificationDevices push_notification_devices()
 */
class Client
{

    /**
     * @var string
     */
    protected $subdomain;
    /**
     * @var string
     */
    protected $username;
    /**
     * @var string
     */
    protected $scheme;
    /**
     * @var string
     */
    protected $hostname;
    /**
     * @var integer
     */
    protected $port;
    /**
     * @var string
     */
    protected $password;
    /**
     * @var string
     */
    protected $token;
    /**
     * @var string
     */
    protected $oAuthToken;
    /**
     * @var string
     */
    protected $apiUrl;
    /**
     * @var string
     */
    protected $apiVer = 'v2';
    /**
     * @var array|null
     */
    protected $sideload;

    /**
     * @var Tickets
     */
    protected $tickets;
    /**
     * @var TicketFields
     */
    protected $ticketFields;
    /**
     * @var TicketForms
     */
    protected $ticketForms;
    /**
     * @var Twitter
     */
    protected $twitter;
    /**
     * @var Attachments
     */
    protected $attachments;
    /**
     * @var Requests
     */
    protected $requests;
    /**
     * @var Users
     */
    protected $users;
    /**
     * @var UserFields
     */
    protected $userFields;
    /**
     * @var Groups
     */
    protected $groups;
    /**
     * @var GroupMemberships
     */
    protected $groupMemberships;
    /**
     * @var CustomRoles
     */
    protected $customRoles;
    /**
     * @var Forums
     */
    protected $forums;
    /**
     * @var Categories
     */
    protected $categories;
    /**
     * @var Topics
     */
    protected $topics;
    /**
     * @var Settings
     */
    protected $settings;
    /**
     * @var ActivityStream
     */
    protected $activityStream;
    /**
     * @var AuditLogs
     */
    protected $auditLogs;
    /**
     * @var Autocomplete
     */
    protected $autocomplete;
    /**
     * @var Automations
     */
    protected $automations;
    /**
     * @var JobStatuses
     */
    protected $jobStatuses;
    /**
     * @var Macros
     */
    protected $macros;
    /**
     * @var DynamicContent
     */
    protected $dynamicContent;
    /**
     * @var OAuthClients
     */
    protected $oauthClients;
    /**
     * @var OAuthTokens
     */
    protected $oauthTokens;
    /**
     * @var OrganizationFields
     */
    protected $organizationFields;
    /**
     * @var Organizations
     */
    protected $organizations;
    /**
     * @var SatisfactionRatings
     */
    protected $satisfactionRatings;
    /**
     * @var Search
     */
    protected $search;
    /**
     * @var SharingAgreements
     */
    protected $sharingAgreements;
    /**
     * @var SuspendedTickets
     */
    protected $suspendedTickets;
    /**
     * @var Tags
     */
    protected $tags;
    /**
     * @var Targets
     */
    protected $targets;
    /**
     * @var Triggers
     */
    protected $triggers;
    /**
     * @var Voice
     */
    protected $voice;
    /**
     * @var Locales
     */
    protected $locales;
    /**
     * @var PushNotificationDevices
     */
    protected $push_notification_devices;
    /**
     * @var Debug
     */
    protected $debug;

    /**
     * @param string $subdomain
     * @param string $username
     */

    public function __construct($subdomain, $username, $scheme = "https", $hostname = "zendesk.com", $port = 443)
    {
        $this->subdomain = $subdomain;
        $this->username = $username;
        $this->hostname = $hostname;
        $this->scheme = $scheme;
        $this->port = $port;

        if (empty($subdomain)) {
            $this->apiUrl = "$scheme://$hostname:$port/api/{$this->apiVer}/";
        } else {
            $this->apiUrl = "$scheme://$subdomain.$hostname:$port/api/{$this->apiVer}/";
        }

        $this->debug = new Debug();
        $this->tickets = new Tickets($this);
        $this->ticketFields = new TicketFields($this);
        $this->ticketForms = new TicketForms($this);
        $this->twitter = new Twitter($this);
        $this->attachments = new Attachments($this);
        $this->requests = new Requests($this);
        $this->views = new Views($this);
        $this->users = new Users($this);
        $this->userFields = new UserFields($this);
        $this->groups = new Groups($this);
        $this->groupMemberships = new GroupMemberships($this);
        $this->customRoles = new CustomRoles($this);
        $this->forums = new Forums($this);
        $this->categories = new Categories($this);
        $this->topics = new Topics($this);
        $this->settings = new Settings($this);
        $this->activityStream = new ActivityStream($this);
        $this->auditLogs = new AuditLogs($this);
        $this->autocomplete = new Autocomplete($this);
        $this->automations = new Automations($this);
        $this->jobStatuses = new JobStatuses($this);
        $this->macros = new Macros($this);
        $this->dynamicContent = new DynamicContent($this);
        $this->oauthClients = new OAuthClients($this);
        $this->oauthTokens = new OAuthTokens($this);
        $this->organizationFields = new OrganizationFields($this);
        $this->organizations = new Organizations($this);
        $this->satisfactionRatings = new SatisfactionRatings($this);
        $this->search = new Search($this);
        $this->sharingAgreements = new SharingAgreements($this);
        $this->suspendedTickets = new SuspendedTickets($this);
        $this->tags = new Tags($this);
        $this->targets = new Targets($this);
        $this->triggers = new Triggers($this);
        $this->voice = new Voice($this);
        $this->locales = new Locales($this);
        $this->push_notification_devices = new PushNotificationDevices($this);
    }

    /**
     * Configure the authorization method
     *
     * @param string $method
     * @param string $value
     */
    public function setAuth($method, $value)
    {
        switch ($method) {
            case 'password':
                $this->password = $value;
                $this->token = '';
                $this->oAuthToken = '';
                break;
            case 'token':
                $this->password = '';
                $this->token = $value;
                $this->oAuthToken = '';
                break;
            case 'oauth_token':
                $this->password = '';
                $this->token = '';
                $this->oAuthToken = $value;
                break;
        }
    }

    /**
     * Returns the supplied subdomain
     *
     * @return string
     */
    public function getSubdomain()
    {
        return $this->subdomain;
    }

    /**
     * Returns the generated api URL
     *
     * @return string
     */
    public function getApiUrl()
    {
        return $this->apiUrl;
    }

    /**
     * Returns a text value indicating the type of authorization configured
     *
     * @return string
     */
    public function getAuthType()
    {
        return ($this->oAuthToken ? 'oauth_token' : ($this->token ? 'token' : 'password'));
    }

    /**
     * Compiles an auth string with either token, password or OAuth credentials
     *
     * @return string
     */
    public function getAuthText()
    {
        return ($this->oAuthToken ? $this->oAuthToken : $this->username . ($this->token ? '/token:' . $this->token : ':' . $this->password));
    }

    /**
     * Set debug information as an object
     *
     * @param mixed $lastRequestHeaders
     * @param mixed $lastResponseCode
     * @param string $lastResponseHeaders
     * @param mixed $lastResponseError
     */
    public function setDebug($lastRequestHeaders, $lastResponseCode, $lastResponseHeaders, $lastResponseError)
    {
        $this->debug->lastRequestHeaders = $lastRequestHeaders;
        $this->debug->lastResponseCode = $lastResponseCode;
        $this->debug->lastResponseHeaders = $lastResponseHeaders;
        $this->debug->lastResponseError = $lastResponseError;
    }

    /**
     * Returns debug information in an object
     *
     * @return Debug
     */
    public function getDebug()
    {
        return $this->debug;
    }

    /**
     * Sideload setter
     *
     * @param array|null $fields
     *
     * @return Client
     */
    public function setSideload(array $fields = null)
    {
        $this->sideload = $fields;

        return $this;
    }

    /**
     * Sideload getter
     *
     * @param array|null $params
     *
     * @return array|null
     */
    public function getSideload(array $params = null)
    {
        return ((isset($params['sideload'])) && (is_array($params['sideload'])) ? $params['sideload'] : $this->sideload);
    }

    /**
     * Generic method to object getter. Since all objects are protected, this method
     * exposes a getter function with the same name as the protected variable, for example
     * $client->tickets can be referenced by $client->tickets()
     *
     * @param $name
     * @param $arguments
     *
     * @throws CustomException
     */
    public function __call($name, $arguments)
    {
        if (isset($this->$name)) {
            return ((isset($arguments[0])) && ($arguments[0] != null) ? $this->$name->setLastId($arguments[0]) : $this->$name);
        }
        $namePlural = $name . 's'; // try pluralize
        if (isset($this->$namePlural)) {
            return $this->$namePlural->setLastId($arguments[0]);
        } else {
            throw new CustomException("No method called $name available in " . __CLASS__);
        }
    }

    /*
     * These ones don't follow the usual construct
     */

    /**
     * @param int $id
     *
     * @return $this
     */
    public function category($id)
    {
        return $this->categories->setLastId($id);
    }

    /**
     * @param int|null $id
     *
     * @return ActivityStream
     */
    public function activities($id = null)
    {
        return ($id != null ? $this->activityStream()->setLastId($id) : $this->activityStream());
    }

    /**
     * @param int $id
     *
     * @return ActivityStream
     */
    public function activity($id)
    {
        return $this->activityStream()->setLastId($id);
    }

    /**
     * @param int $id
     *
     * @return JobStatuses
     */
    public function jobStatus($id)
    {
        return $this->jobStatuses()->setLastId($id);
    }

    /**
     * @param array $params
     *
     * @throws MissingParametersException
     * @throws ResponseException
     *
     * @return mixed
     */
    public function search(array $params)
    {
        return $this->search->performSearch($params);
    }

    /**
     * @param array $params
     *
     * @throws MissingParametersException
     * @throws ResponseException
     *
     * @return mixed
     */
    public function anonymousSearch(array $params)
    {
        return $this->search->anonymousSearch($params);
    }

}