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

FilestoragePager::getSlice()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 18
rs 9.9
c 1
b 0
f 1
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