Client::initializer()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 27
rs 8.439
cc 5
eloc 14
nc 1
nop 1
1
<?php
2
3
/*
4
 * This is part of the webuni/srazy-api-client.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\Srazy;
14
15
use GuzzleHttp\Client as HttpClient;
16
use GuzzleHttp\Psr7;
17
use ProxyManager\Proxy\GhostObjectInterface;
18
19
class Client
20
{
21
    private $httpClient;
22
    private $manager;
23
    private $baseUri;
24
25
    public function __construct(HttpClient $httpClient = null, Manager $manager = null)
26
    {
27
        $this->httpClient = $httpClient ?: new HttpClient();
28
        $this->manager = $manager ?: new Manager();
29
        $this->baseUri = Psr7\uri_for('http://srazy.info');
30
    }
31
32
    public function series()
33
    {
34
        return new Api\SeriesApi($this, $this->manager);
0 ignored issues
show
Unused Code introduced by
The call to SeriesApi::__construct() has too many arguments starting with $this->manager.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
35
    }
36
37
    public function user()
38
    {
39
        return new Api\UserApi($this, $this->manager);
0 ignored issues
show
Unused Code introduced by
The call to UserApi::__construct() has too many arguments starting with $this->manager.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
40
    }
41
42
    public function event()
43
    {
44
        return new Api\EventApi($this, $this->manager);
0 ignored issues
show
Unused Code introduced by
The call to EventApi::__construct() has too many arguments starting with $this->manager.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
    }
46
47
    public function api($type)
48
    {
49
        if (class_exists($type)) {
50
            $type = (new \ReflectionClass($type))->getShortName();
51
        }
52
53
        return $this->{$type}();
54
    }
55
56
    public function ajax($url, array $options = [], \Closure $processor = null)
57
    {
58
        $options['headers'] = array_merge(isset($options['headers']) ? $options['headers'] : [], ['X-Requested-With' => 'XMLHttpRequest']);
59
60
        return $this->get($url, $options, $processor);
61
    }
62
63
    public function get($url, array $options = [], \Closure $processor = null)
64
    {
65
        $url = Psr7\Uri::resolve($this->baseUri, $url);
66
        $response = $this->httpClient->get($url, $options);
67
68
        if (null !== $processor) {
69
            $response = $processor($response);
70
        }
71
72
        $crawler = new Crawler(null, (string) $url, (string) $this->baseUri);
73
        $crawler->addContent($response->getBody());
74
75
        return $crawler;
76
    }
77
78
    public function model($class, $uri)
79
    {
80
        return $this->manager->get($class, $uri, $this->initializer($class));
81
    }
82
83
    private function initializer($class)
84
    {
85
        $initializer = function (GhostObjectInterface &$wrappedObject, $method, array $parameters) use ($class) {
86
            if (!method_exists($wrappedObject, $method)) {
87
                return;
88
            }
89
90
            if (0 === strpos($method, 'set')) {
91
                return;
92
            }
93
94
            if (null !== call_user_func([$wrappedObject, $method], $parameters)) {
95
                return;
96
            }
97
98
            $api = $this->api($class);
99
            if (method_exists($api, $method)) {
100
                $api->{$method}($wrappedObject);
101
102
                return;
103
            }
104
105
            $wrappedObject = $api->get($wrappedObject);
106
        };
107
108
        return $initializer;
109
    }
110
}
111