1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Syncer\InvoiceNinja; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use JMS\Serializer\SerializerInterface; |
7
|
|
|
use Syncer\Dto\InvoiceNinja\Task; |
8
|
|
|
use Syncer\Dto\InvoiceNinja\Client; |
9
|
|
|
use Syncer\Dto\InvoiceNinja\Project; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class InvoiceNinjaClient |
13
|
|
|
* @package Syncer\InvoiceNinja |
14
|
|
|
* |
15
|
|
|
* @author Matthieu Calie <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class InvoiceNinjaClient |
18
|
|
|
{ |
19
|
|
|
const VERSION = 'v1'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var GuzzleClient |
23
|
|
|
*/ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var SerializerInterface |
28
|
|
|
*/ |
29
|
|
|
private $serializer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $api_token; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Client constructor. |
38
|
|
|
* |
39
|
|
|
* @param GuzzleClient $client |
40
|
|
|
* @param SerializerInterface $serializer |
41
|
|
|
* @param $api_token |
42
|
|
|
*/ |
43
|
|
|
public function __construct(GuzzleClient $client, SerializerInterface $serializer, $api_token) |
44
|
|
|
{ |
45
|
|
|
$this->client = $client; |
46
|
|
|
$this->serializer = $serializer; |
47
|
|
|
$this->api_token = $api_token; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Task $task |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function saveNewTask(Task $task) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$data = $this->serializer->serialize($task, 'json'); |
58
|
|
|
|
59
|
|
|
$res = $this->client->request('POST', self::VERSION . '/tasks', [ |
60
|
|
|
'body' => $data, |
61
|
|
|
'headers' => [ |
62
|
|
|
'Content-Type' => 'application/json', |
63
|
|
|
'X-Ninja-Token' => $this->api_token, |
64
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
65
|
|
|
] |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
return $this->serializer->deserialize($res->getBody(), Task::class, 'json'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Project $project |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function saveNewProject(Project $project) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$data = $this->serializer->serialize($project, 'json'); |
79
|
|
|
|
80
|
|
|
$res = $this->client->request('POST', self::VERSION . '/projects', [ |
81
|
|
|
'body' => $data, |
82
|
|
|
'headers' => [ |
83
|
|
|
'Content-Type' => 'application/json', |
84
|
|
|
'X-Ninja-Token' => $this->api_token, |
85
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
86
|
|
|
] |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
return $this->serializer->deserialize($res->getBody(), 'Syncer\Dto\InvoiceNinja\ProjectWrapper', 'json')->getData(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Project $project |
|
|
|
|
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function saveNewClient(Client $client) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$data = $this->serializer->serialize($client, 'json'); |
100
|
|
|
|
101
|
|
|
$res = $this->client->request('POST', self::VERSION . '/clients', [ |
102
|
|
|
'body' => $data, |
103
|
|
|
'headers' => [ |
104
|
|
|
'Content-Type' => 'application/json', |
105
|
|
|
'X-Ninja-Token' => $this->api_token, |
106
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
107
|
|
|
] |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
return $this->serializer->deserialize($res->getBody(), 'Syncer\Dto\InvoiceNinja\ClientWrapper', 'json')->getData(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array|Project[] |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function getProjects() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$response = $this->client->request('GET', self::VERSION . '/projects', [ |
119
|
|
|
'headers' => [ |
120
|
|
|
'Content-Type' => 'application/json', |
121
|
|
|
'X-Ninja-Token' => $this->api_token, |
122
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
123
|
|
|
] |
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
return $this->serializer->deserialize($response->getBody(), 'Syncer\Dto\InvoiceNinja\ProjectsWrapper', 'json')->getData(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array|Client[] |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function getClients() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$response = $this->client->request('GET', self::VERSION . '/clients', [ |
135
|
|
|
'headers' => [ |
136
|
|
|
'Content-Type' => 'application/json', |
137
|
|
|
'X-Ninja-Token' => $this->api_token, |
138
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
139
|
|
|
] |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
return $this->serializer->deserialize($response->getBody(), 'Syncer\Dto\InvoiceNinja\ClientsWrapper', 'json')->getData(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.