Completed
Push — master ( 88af56...75810a )
by
unknown
12s
created

AbstractRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 67
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getAdapter() 0 4 1
B query() 0 23 5
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;
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...
76 13
                case 401:
77 12
                    throw new UnauthorizedException(sprintf('Access to the following resource %s is forbidden', $path));
78
                    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...
79 1
                case 400:
80
                    throw new TokenExpiredException('Token is expired or email validation is already done');
81
                    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...
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