Completed
Pull Request — master (#64)
by Thibaud
03:05
created

AbstractRepository::query()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.2
cc 4
eloc 15
nc 4
nop 5
crap 4
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\UnauthorizedException;
18
use PhraseanetSDK\Exception\RuntimeException;
19
use PhraseanetSDK\Http\APIResponse;
20
use PhraseanetSDK\Http\APIGuzzleAdapter;
21
22
abstract class AbstractRepository
23
{
24
    /**
25
     * @var EntityManager
26
     */
27
    protected $em;
28
29
    /**
30
     * @var APIGuzzleAdapter
31
     */
32
    private $adapter;
33
34
    /**
35
     * @param EntityManager $em
36
     * @param APIGuzzleAdapter $adapter
37
     */
38 85
    public function __construct(EntityManager $em, APIGuzzleAdapter $adapter = null)
39
    {
40 85
        $this->em = $em;
41 85
        $this->adapter = $adapter ?: $this->em->getAdapter();
42 85
    }
43
44
    /**
45
     * @return APIGuzzleAdapter
46
     */
47 68
    private function getAdapter()
48
    {
49 68
        return $this->adapter;
50
    }
51
52
    /**
53
     * Query the API
54
     *
55
     * @param string $method HTTP method type (POST, GET ...)
56
     * @param string $path The requested path (/path/to/ressource/1)
57
     * @param array $query An array of query parameters
58
     * @param array $postFields An array of request parameters
59
     * @param array $headers
60
     *
61
     * @return APIResponse
62
     * @throws NotFoundException
63
     * @throws UnauthorizedException
64
     */
65 68
    protected function query($method, $path, $query = array(), $postFields = array(), array $headers = array())
66
    {
67
        try {
68 68
            $response = $this->getAdapter()->call($method, $path, $query, $postFields, array(), $headers);
69 68
        } catch (BadResponseException $e) {
70 14
            $statusCode = $e->getStatusCode();
71
            switch ($statusCode) {
72 14
                case 404:
73 1
                    throw new NotFoundException(sprintf('Resource under %s could not be found', $path));
74
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
75 13
                case 401:
76 12
                    throw new UnauthorizedException(sprintf('Access to the following resource %s is forbidden', $path));
77
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
78 1
                default:
79 1
                    throw new RuntimeException(sprintf('Something went wrong "%s"', $e->getMessage()));
80 1
            }
81
        }
82
83 52
        return $response;
84
    }
85
}
86