Client::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
crap 2
1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\MyAnimeListSyncBundle\Service;
12
13
use Guzzle\Http\Message\Response;
14
use Guzzle\Http\Client as ClientHttp;
15
use Guzzle\Http\Exception\BadResponseException;
16
17
/**
18
 * MyAnimeList client
19
 *
20
 * @package AnimeDb\Bundle\MyAnimeListSyncBundle\Service
21
 * @author  Peter Gribanov <[email protected]>
22
 */
23
class Client
24
{
25
    /**
26
     * @var ClientHttp
27
     */
28
    protected $client;
29
30
    /**
31
     * @var string
32
     */
33
    protected $api_key = '';
34
35
    /**
36
     * @var string
37
     */
38
    protected $user_name = '';
39
40
    /**
41
     * @var string
42
     */
43
    protected $user_password = '';
44
45
    const ACTION_ADD = 'add';
46
    const ACTION_UPDATE = 'update';
47
    const ACTION_DELETE = 'delete';
48
49
    /**
50
     * @param ClientHttp $client
51
     * @param string $api_key
52
     * @param string $user_name
53
     * @param string $user_password
54
     */
55
    public function __construct(ClientHttp $client, $api_key, $user_name, $user_password)
56
    {
57
        $this->client = $client;
58
        $this->api_key = $api_key;
59
        $this->user_name = $user_name;
60
        $this->user_password = $user_password;
61
    }
62
63
    /**
64
     * @param string $action add|update|delete
65
     * @param integer $id
66
     * @param string|null $data
67
     *
68
     * @return Response|null
69
     */
70
    public function sendAction($action, $id, $data = null)
71
    {
72
        try {
73
            $request = $this->client->post(
74
                sprintf('animelist/%s/%s.xml', $action, $id),
75
                null,
76
                $data ? ['data' => $data] : []
77
            );
78
            $request->setAuth($this->user_name, $this->user_password);
79
            $request->setHeader('User-Agent', 'api-team-'.$this->api_key);
80
            return $request->send();
81
82
        } catch (BadResponseException $e) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Http\Exception\BadResponseException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
83
            // is not a critical error
84
            return null;
85
        }
86
    }
87
88
    /**
89
     * @param string $query
90
     *
91
     * @return int|null
92
     */
93
    public function search($query)
94
    {
95
        $request = $this->client->get('anime/search.xml');
96
        $request->setAuth($this->user_name, $this->user_password);
97
        $request->setHeader('User-Agent', 'api-team-'.$this->api_key);
98
        $request->getQuery()->set('q', $query);
99
100
        try {
101
            $data = $request->send()->getBody(true);
102
103
            if ($data != 'No results') {
104
                $doc = new \DOMDocument();
105
                $doc->loadXML(html_entity_decode($data));
106
                $xpath = new \DOMXPath($doc);
107
                $ids = $xpath->query('entry/id');
108
109
                if ($ids->length == 1) {
110
                    return (int)$ids->item(0)->nodeValue;
111
                }
112
            }
113
114
        } catch (BadResponseException $e) {
0 ignored issues
show
Bug introduced by
The class Guzzle\Http\Exception\BadResponseException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
115
            // is not a critical error
116
        }
117
118
        return null;
119
    }
120
}
121