Completed
Push — master ( 1c21c5...279fbd )
by Pieter
16s queued 11s
created

FilestoragePager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 24
dl 0
loc 57
rs 10
c 1
b 0
f 1
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNbResults() 0 7 2
A __construct() 0 6 1
A getSlice() 0 18 3
1
<?php
2
3
namespace W2w\Lib\Apie\Plugins\FileStorage\Pagers;
4
5
use Iterator;
6
use LimitIterator;
7
use OutOfBoundsException;
8
use Pagerfanta\Adapter\AdapterInterface;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Finder\SplFileInfo;
11
use W2w\Lib\Apie\Plugins\FileStorage\DataLayers\FileStorageDataLayer;
12
13
class FilestoragePager implements AdapterInterface
14
{
15
    /**
16
     * @var FileStorageDataLayer
17
     */
18
    private $dataLayer;
19
20
    /**
21
     * @var Iterator
22
     */
23
    private $iterator;
24
25
    /**
26
     * @var string
27
     */
28
    private $resourceClass;
29
30
    /**
31
     * @var array
32
     */
33
    private $context;
34
35
    public function __construct(FileStorageDataLayer $dataLayer, Iterator $iterator, string $resourceClass, array $context)
36
    {
37
        $this->dataLayer = $dataLayer;
38
        $this->iterator = $iterator;
39
        $this->resourceClass = $resourceClass;
40
        $this->context = $context;
41
    }
42
43
    public function getNbResults()
44
    {
45
        $count = 0;
46
        for ($this->iterator->rewind(); $this->iterator->valid(); $this->iterator->next()) {
47
            $count++;
48
        }
49
        return $count;
50
    }
51
52
    public function getSlice($offset, $length)
53
    {
54
        $list = new LimitIterator(
55
            $this->iterator,
56
            $offset,
57
            $length
58
        );
59
        $result = [];
60
        try {
61
            foreach ($list as $file) {
62
                /** @var SplFileInfo $file */
63
                $result[] = $this->dataLayer->retrieve($this->resourceClass, $file->getBasename(), $this->context);
64
65
            }
66
        } catch (OutOfBoundsException $outOfBoundsException) {
67
            $outOfBoundsException->getMessage(); // ignore
68
        }
69
        return $result;
70
    }
71
}
72