Passed
Push — master ( 1282f8...92a01c )
by Schlaefer
03:31
created

PageCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 63.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 81
ccs 19
cts 30
cp 0.6333
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 5 1
A __construct() 0 4 1
A getIterator() 0 5 1
A offsetExists() 0 5 1
A offsetGet() 0 5 1
A offsetSet() 0 5 1
A offsetUnset() 0 5 1
A count() 0 5 1
A load() 0 6 2
1
<?php
2
3
namespace Phile\Repository;
4
5
/**
6
 * Page collection which delays searching for and loading pages until necessary.
7
 *
8
 * @author  PhileCMS
9
 * @link    https://philecms.com
10
 * @license http://opensource.org/licenses/MIT
11
 * @package Phile\Repository
12
 */
13
class PageCollection implements \ArrayAccess, \IteratorAggregate, \Countable
14
{
15
    /**
16
     * @var callback A function to be used for loading the pages.
17
     */
18
    private $loader;
19
20
    /**
21
     * @var array of \Phile\Model\Page
22
     */
23
    private $pages;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param callable $loader pages loader
29
     */
30 9
    public function __construct(callable $loader)
31
    {
32 9
        $this->loader = $loader;
33 9
    }
34
35
    /**
36
     * Perform page loading.
37
     *
38
     * @return void
39
     */
40 9
    private function load()
41
    {
42 9
        if ($this->pages === null) {
43 9
            $this->pages = call_user_func($this->loader);
0 ignored issues
show
Documentation Bug introduced by
It seems like call_user_func($this->loader) of type * is incompatible with the declared type array of property $pages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
        }
45 8
    }
46
47
    /**
48
     * Get pages in a array.
49
     *
50
     * @return array of \Phile\Model\Page
51
     */
52 2
    public function toArray()
53
    {
54 2
        $this->load();
55 1
        return $this->pages;
56
    }
57
58 4
    public function getIterator()
59
    {
60 4
        $this->load();
61 4
        return new \ArrayIterator($this->pages);
62
    }
63
64
    public function offsetExists($offset)
65
    {
66
        $this->load();
67
        return isset($this->pages[$offset]);
68
    }
69
70 2
    public function offsetGet($offset)
71
    {
72 2
        $this->load();
73 2
        return $this->pages[$offset];
74
    }
75
76
    public function offsetSet($offset, $value)
77
    {
78
        $this->load();
79
        $this->pages[$offset] =    $value;
80
    }
81
82
    public function offsetUnset($offset)
83
    {
84
        $this->load();
85
        unset($this->pages[$offset]);
86
    }
87
88 2
    public function count()
89
    {
90 2
        $this->load();
91 2
        return count($this->pages);
92
    }
93
}
94