1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Phraseanet SDK. |
5
|
|
|
* |
6
|
|
|
* (c) Alchemy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhraseanetSDK; |
13
|
|
|
|
14
|
|
|
use PhraseanetSDK\EntityManager; |
15
|
|
|
use PhraseanetSDK\Exception\BadResponseException; |
16
|
|
|
use PhraseanetSDK\Exception\NotFoundException; |
17
|
|
|
use PhraseanetSDK\Exception\TokenExpiredException; |
18
|
|
|
use PhraseanetSDK\Exception\UnauthorizedException; |
19
|
|
|
use PhraseanetSDK\Exception\RuntimeException; |
20
|
|
|
use PhraseanetSDK\Http\APIResponse; |
21
|
|
|
use PhraseanetSDK\Http\APIGuzzleAdapter; |
22
|
|
|
|
23
|
|
|
abstract class AbstractRepository |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EntityManager |
27
|
|
|
*/ |
28
|
|
|
protected $em; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var APIGuzzleAdapter |
32
|
|
|
*/ |
33
|
|
|
private $adapter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param EntityManager $em |
37
|
|
|
* @param APIGuzzleAdapter $adapter |
38
|
|
|
*/ |
39
|
85 |
|
public function __construct(EntityManager $em, APIGuzzleAdapter $adapter = null) |
40
|
|
|
{ |
41
|
85 |
|
$this->em = $em; |
42
|
85 |
|
$this->adapter = $adapter ?: $this->em->getAdapter(); |
43
|
85 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return APIGuzzleAdapter |
47
|
|
|
*/ |
48
|
68 |
|
private function getAdapter() |
49
|
|
|
{ |
50
|
68 |
|
return $this->adapter; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Query the API |
55
|
|
|
* |
56
|
|
|
* @param string $method HTTP method type (POST, GET ...) |
57
|
|
|
* @param string $path The requested path (/path/to/ressource/1) |
58
|
|
|
* @param array $query An array of query parameters |
59
|
|
|
* @param array $postFields An array of request parameters |
60
|
|
|
* @param array $headers |
61
|
|
|
* |
62
|
|
|
* @return APIResponse |
63
|
|
|
* @throws NotFoundException |
64
|
|
|
* @throws UnauthorizedException |
65
|
|
|
*/ |
66
|
68 |
|
protected function query($method, $path, $query = array(), $postFields = array(), array $headers = array()) |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
68 |
|
$response = $this->getAdapter()->call($method, $path, $query, $postFields, array(), $headers); |
70
|
68 |
|
} catch (BadResponseException $e) { |
71
|
14 |
|
$statusCode = $e->getStatusCode(); |
72
|
|
|
switch ($statusCode) { |
73
|
14 |
|
case 404: |
74
|
1 |
|
throw new NotFoundException(sprintf('Resource under %s could not be found', $path)); |
75
|
|
|
break; |
|
|
|
|
76
|
13 |
|
case 401: |
77
|
12 |
|
throw new UnauthorizedException(sprintf('Access to the following resource %s is forbidden', $path)); |
78
|
|
|
break; |
|
|
|
|
79
|
1 |
|
case 400: |
80
|
|
|
throw new TokenExpiredException('Token is expired or email validation is already done'); |
81
|
|
|
break; |
|
|
|
|
82
|
1 |
|
default: |
83
|
1 |
|
throw new RuntimeException(sprintf('Something went wrong "%s"', $e->getMessage())); |
84
|
1 |
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
52 |
|
return $response; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.