PageCollection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 65.38%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 20
c 3
b 0
f 2
dl 0
loc 79
ccs 17
cts 26
cp 0.6538
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 4 1
A offsetSet() 0 4 1
A getIterator() 0 4 1
A offsetGet() 0 4 1
A __construct() 0 3 1
A count() 0 4 1
A offsetUnset() 0 4 1
A load() 0 4 2
A offsetExists() 0 4 1
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.github.io
10
 * @license http://opensource.org/licenses/MIT
11
 * @package Phile\Repository
12
 */
13
class PageCollection implements \ArrayAccess, \IteratorAggregate, \Countable
14
{
15
    /**
16
     * @var callable 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 13
    public function __construct(callable $loader)
31
    {
32 13
        $this->loader = $loader;
33
    }
34
35
    /**
36
     * Perform page loading.
37
     *
38
     * @return void
39
     */
40 13
    private function load()
41
    {
42 13
        if ($this->pages === null) {
43 13
            $this->pages = call_user_func($this->loader);
44
        }
45
    }
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 8
    public function getIterator(): \Traversable
59
    {
60 8
        $this->load();
61 8
        return new \ArrayIterator($this->pages);
62
    }
63
64
    public function offsetExists($offset): bool
65
    {
66
        $this->load();
67
        return isset($this->pages[$offset]);
68
    }
69
70 2
    public function offsetGet($offset): mixed
71
    {
72 2
        $this->load();
73 2
        return $this->pages[$offset];
74
    }
75
76
    public function offsetSet($offset, $value): void
77
    {
78
        $this->load();
79
        $this->pages[$offset] = $value;
80
    }
81
82
    public function offsetUnset($offset): void
83
    {
84
        $this->load();
85
        unset($this->pages[$offset]);
86
    }
87
88 2
    public function count(): int
89
    {
90 2
        $this->load();
91 2
        return count($this->pages);
92
    }
93
}
94