OffsetAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 4 1
A logic() 0 15 4
1
<?php
2
3
/*
4
 * This file is part of the SomeWork/OffsetPage package.
5
 *
6
 * (c) Pinchuk Igor <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace SomeWork\OffsetPage;
13
14
use SomeWork\OffsetPage\Logic\AlreadyGetNeededCountException;
15
use SomeWork\OffsetPage\Logic\Offset;
16
17
class OffsetAdapter
18
{
19
    /**
20
     * @var SourceInterface
21
     */
22
    protected $source;
23
24
    public function __construct(SourceInterface $source)
25
    {
26
        $this->source = $source;
27
    }
28
29
    /**
30
     * @param int $offset
31
     * @param int $limit
32
     * @param int $nowCount
33
     *
34
     * @throws \LogicException
35
     *
36
     * @return \SomeWork\OffsetPage\OffsetResult
37
     */
38
    public function execute($offset, $limit, $nowCount = 0)
39
    {
40
        return new OffsetResult($this->logic($offset, $limit, $nowCount));
41
    }
42
43
    /**
44
     * @param $offset
45
     * @param $limit
46
     * @param $nowCount
47
     *
48
     * @throws \LogicException
49
     *
50
     * @return \Generator
51
     */
52
    protected function logic($offset, $limit, $nowCount)
53
    {
54
        try {
55
            while ($offsetResult = Offset::logic($offset, $limit, $nowCount)) {
56
                $result = $this->source->execute($offsetResult->getPage(), $offsetResult->getSize());
57
                if ($result->getTotalCount() === 0) {
58
                    return;
59
                }
60
                $nowCount += $result->getTotalCount();
61
                yield $result;
62
            }
63
        } catch (AlreadyGetNeededCountException $exception) {
64
            return;
65
        }
66
    }
67
}
68