Completed
Push — master ( b1e1b8...e19f3c )
by Hans
02:15
created

PagedList   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 60%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 11
c 4
b 0
f 2
lcom 1
cbo 0
dl 0
loc 99
ccs 12
cts 20
cp 0.6
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A items() 0 4 1
A hasNext() 0 4 1
A getNextUrl() 0 4 1
A guardThatTheseAreAllPinterestObjects() 0 11 4
A assertValidUri() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the Pinterest PHP library.
5
 *
6
 * (c) Hans Ott <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 *
11
 * Source: https://github.com/hansott/pinterest-php
12
 */
13
14
namespace Pinterest\Objects;
15
16
use InvalidArgumentException;
17
use Pinterest\Authentication;
18
19
/**
20
 * This class represents a paged list.
21
 *
22
 * @author Hans Ott <[email protected]>
23
 */
24
final class PagedList
25
{
26
    /**
27
     * The paged list items.
28
     *
29
     * @var array
30
     */
31
    private $items;
32
33
    /**
34
     * The url for retrieving the next set of items.
35
     *
36
     * @var string
37
     */
38
    private $nextUrl;
39
40
    /**
41
     * Creates a new paged list.
42
     *
43
     * @param array  $items   The paged list items.
44
     * @param string $nextUrl The url for retrieving the next set of items.
45 14
     */
46
    public function __construct(array $items = array(), $nextUrl = null)
47 14
    {
48 14
        $this->guardThatTheseAreAllPinterestObjects($items);
49 14
        $this->assertValidUri($nextUrl);
50 14
        $this->items = $items;
51
        $this->nextUrl = $nextUrl;
52
    }
53
54
    /**
55
     * Returns the items.
56
     *
57 14
     * @return array The items.
58
     */
59 14
    public function items()
60
    {
61
        return $this->items;
62
    }
63
64
    /**
65
     * Returns whether the paged list has more items.
66
     *
67
     * @return bool Whether there are more items.
68
     */
69
    public function hasNext()
70
    {
71
        return !empty($this->nextUrl);
72
    }
73
74
    /**
75
     * Returns the url to get the next set of items.
76
     *
77
     * @return string The url to get the next set of items.
78
     */
79
    public function getNextUrl()
80
    {
81
        return $this->nextUrl;
82
    }
83
84
    /**
85
     * Checks if all items are pinterest objects.
86
     *
87
     * @param array $items
88
     *
89 14
     * @throws InvalidArgumentException
90
     */
91 14
    private function guardThatTheseAreAllPinterestObjects(array $items)
92 12
    {
93
        foreach ($items as $item) {
94
            if (! ($item instanceof BaseObject)) {
95
                throw new InvalidArgumentException(sprintf(
96
                    'Expected "Pinterest\Objects\BaseObject" but instead got: "%s"',
97
                    is_object($item) ? get_class($item) : gettype($item)
98 14
                ));
99 14
            }
100
        }
101
    }
102
103
    /**
104
     * Checks if the next uri is valid.
105
     *
106
     * @throws InvalidArgumentException
107
     *
108
     * @param $nextUri
109
     */
110
    private function assertValidUri($nextUri)
111
    {
112
        if ($nextUri === null) {
113
            return;
114
        }
115
116
        if (strpos($nextUri, Authentication::BASE_URI) === false) {
117
            throw new InvalidArgumentException(
118
                'Not a pinterest api uri'
119
            );
120
        }
121
    }
122
}
123