Completed
Push — master ( bc8abc...611a53 )
by Achille
9s
created

Box::findAll()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 8.439
cc 5
eloc 14
nc 9
nop 3
1
<?php
2
3
namespace Streak\Endpoint;
4
5
use Streak\Paginator;
6
7
class Box extends AbstractEndpoint
8
{
9
    const ENDPOINT = 'boxes';
10
11
    public function all()
12
    {
13
        return $this->client->get(self::ENDPOINT);
14
    }
15
16
    public function findAll($pipelineKey, $sortBy = null, Paginator $paginator = null)
17
    {
18
        $options = ['query' => []];
19
20
        if (null !== $sortBy) {
21
            if (!in_array($sortBy, ['creationTimestamp', 'lastUpdatedTimestamp'])) {
22
                throw new \InvalidArgumentException('Invalid sort field.');
23
            }
24
25
            $options['query']['sortBy'] = $sortBy;
26
        }
27
28
        if (null === $paginator) {
29
            $paginator = new Paginator();
30
        }
31
32
        while ($paginator->nextPage()) {
33
            $options['query']['page'] = $paginator->getPage();
34
            $options['query']['limit'] = $paginator->getLimit();
35
36
            $results = $this->client->get(sprintf('pipelines/%s/%s', $pipelineKey, self::ENDPOINT), $options);
37
38
            $paginator->addResults($results);
0 ignored issues
show
Bug introduced by
It seems like $results defined by $this->client->get(sprin...f::ENDPOINT), $options) on line 36 can also be of type string; however, Streak\Paginator::addResults() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39
        }
40
41
        return $paginator->getResults();
42
    }
43
44 View Code Duplication
    public function create($pipelineKey, array $box)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
45
    {
46
        if (!isset($box['name'])) {
47
            throw new \InvalidArgumentException('Missing required fields.');
48
        }
49
50
        return $this->client->put(sprintf('pipelines/%s/%s', $pipelineKey, self::ENDPOINT), [
51
            'form_params' => $box,
52
        ]);
53
    }
54
55
    public function delete($boxKey)
56
    {
57
        return $this->client->delete(sprintf('%s/%s', self::ENDPOINT, $boxKey));
58
    }
59
60 View Code Duplication
    public function edit($boxKey, array $box)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
61
    {
62
        foreach ($box as $field => $value) {
63
            if (!in_array($field, ['name', 'notes', 'stageKey', 'followerKeys', 'fields'])) {
64
                throw new \InvalidArgumentException('Not allowed field.');
65
            }
66
        }
67
68
        return $this->client->post(sprintf('%s/%s', self::ENDPOINT, $boxKey), [
69
            'json' => $box,
70
        ]);
71
    }
72
73
    public function find($boxKey)
74
    {
75
        return $this->client->get(sprintf('%s/%s', self::ENDPOINT, $boxKey));
76
    }
77
}
78