Browser   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 111
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B createFromOptions() 0 27 2
A getColumns() 0 6 1
A getColumnsForDisplay() 0 10 2
A getCurrentColumn() 0 4 1
A getPath() 0 4 1
A getMaxColumns() 0 4 1
A getColumnsForPaths() 0 11 2
B getColumnPaths() 0 22 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ResourceBrowser;
6
7
use Psi\Component\ResourceBrowser\Filter\Filter\PassThroughFilter;
8
use Psi\Component\ResourceBrowser\Filter\FilterInterface;
9
use Puli\Repository\Api\ResourceRepository;
10
11
final class Browser
12
{
13
    private $repository;
14
    private $nbColumns;
15
    private $path;
16
    private $filter;
17
18
    private function __construct(ResourceRepository $repository, FilterInterface $filter, string $path, int $nbColumns)
19
    {
20
        $this->repository = $repository;
21
        $this->nbColumns = $nbColumns;
22
        $this->path = $path;
23
        $this->filter = $filter;
24
    }
25
26
    public static function createFromOptions(ResourceRepository $repository, array $options = [])
27
    {
28
        $defaultOptions = [
29
            'path' => '/',
30
            'nb_columns' => 4,
31
            'filter' => new PassThroughFilter(),
32
        ];
33
34
        if ($diff = array_diff(array_keys($options), array_keys($defaultOptions))) {
35
            throw new \InvalidArgumentException(sprintf(
36
                'Invalid options: "%s". Valid options: "%s"',
37
                implode('", "', $diff),
38
                implode('", "', array_keys($defaultOptions))
39
            ));
40
        }
41
42
        $options = array_merge($defaultOptions, $options);
43
44
        $browser = new self(
45
            $repository,
46
            $options['filter'],
47
            $options['path'],
48
            $options['nb_columns']
49
        );
50
51
        return $browser;
52
    }
53
54
    public function getColumns()
55
    {
56
        $columnPaths = $this->getColumnPaths($this->path);
57
58
        return $this->getColumnsForPaths($columnPaths);
59
    }
60
61
    public function getColumnsForDisplay()
62
    {
63
        $columnPaths = $this->getColumnPaths($this->path);
64
65
        if (count($columnPaths) > $this->nbColumns) {
66
            $columnPaths = array_slice($columnPaths, -$this->nbColumns);
67
        }
68
69
        return $this->getColumnsForPaths($columnPaths);
70
    }
71
72
    public function getCurrentColumn()
73
    {
74
        return new Column($this->repository->get($this->path), $this->filter);
75
    }
76
77
    public function getPath()
78
    {
79
        return $this->path;
80
    }
81
82
    public function getMaxColumns()
83
    {
84
        return $this->nbColumns;
85
    }
86
87
    private function getColumnsForPaths(array $columnPaths)
88
    {
89
        $columns = [];
90
91
        foreach ($columnPaths as $columnPath) {
92
            $resource = $this->repository->get($columnPath);
93
            $columns[] = new Column($resource, $this->filter);
94
        }
95
96
        return $columns;
97
    }
98
99
    private function getColumnPaths($path)
100
    {
101
        $columnNames = ['/'];
102
103
        if ($path !== '/') {
104
            $columnNames = explode('/', ltrim($path, '/'));
105
            array_unshift($columnNames, '/');
106
        }
107
108
109
        $columnPaths = [];
110
111
        foreach ($columnNames as $columnName) {
112
            if ($columnName !== '/') {
113
                $elements[] = $columnName;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$elements was never initialized. Although not strictly required by PHP, it is generally a good practice to add $elements = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
114
            }
115
116
            $columnPaths[] = empty($elements) ? '/' : '/' . implode('/', $elements);
117
        }
118
119
        return $columnPaths;
120
    }
121
}
122