Completed
Pull Request — master (#90)
by Arnaud
03:24
created

EntityLoader::loadPaginate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 0
cts 12
cp 0
rs 8.8571
cc 3
eloc 12
nc 3
nop 4
crap 12
1
<?php
2
3
namespace LAG\AdminBundle\DataProvider\Loader;
4
5
use Doctrine\Common\Collections\Collection;
6
use LAG\AdminBundle\Action\Configuration\ActionConfiguration;
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use LAG\AdminBundle\DataProvider\DataProviderInterface;
9
use LAG\AdminBundle\Pager\PagerfantaAdminAdapter;
10
use LogicException;
11
use Pagerfanta\Pagerfanta;
12
13
/**
14
 * The EntityLoader is responsible for loading one or multiple entities from the data provider, and if a pagination
15
 * system is required.
16
 */
17
class EntityLoader implements EntityLoaderInterface
18
{
19
    /**
20
     * True if a pagination system is required for the current Action.
21
     *
22
     * @var bool
23
     */
24
    private $isPaginationRequired = false;
25
    
26
    /**
27
     * The name of the pagination system. Only pagerfanta is yet supported.
28
     *
29
     * @var string
30
     */
31
    private $pagerName;
32
    
33
    /**
34
     * The loading strategy
35
     *
36
     * @see AdminInterface
37
     *
38
     * @var string
39
     */
40
    private $loadStrategy;
41
    
42
    /**
43
     * The provider used to retrieve data.
44
     *
45
     * @var DataProviderInterface
46
     */
47
    private $dataProvider;
48
    
49
    /**
50
     * EntityLoader constructor.
51
     *
52
     * @param DataProviderInterface $dataProvider
53
     */
54 1
    public function __construct(DataProviderInterface $dataProvider)
55
    {
56 1
        $this->dataProvider = $dataProvider;
57 1
    }
58
    
59
    /**
60
     * @param ActionConfiguration $configuration
61
     */
62
    public function configure(ActionConfiguration $configuration)
63
    {
64
        $this->isPaginationRequired =
65
            AdminInterface::LOAD_STRATEGY_MULTIPLE !== $configuration->getParameter('load_strategy');
66
    }
67
    
68
    /***
69
     * @param array $criteria
70
     * @param array $orderBy
71
     * @param int   $limit
72
     * @param int   $offset
73
     *
74
     * @return array|Collection|Pagerfanta
75
     */
76
    public function load(array $criteria, array $orderBy = [], $limit = 25, $offset = 1)
77
    {
78
        if (false === $this->isPaginationRequired) {
79
            $limit = null;
80
            $offset = null;
81
            // load entities from the DataProvider using a pagination system
82
            $entities = $this->loadPaginate($criteria, $orderBy, $limit, $offset);
83
        }
84
        else {
85
            // if no pagination is required (edit action for example)
86
            $entities = $this->loadWithoutPagination($criteria, $orderBy);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->loadWithoutPagina...n($criteria, $orderBy); of type array|Doctrine\Common\Collections\Collection adds the type array to the return on line 90 which is incompatible with the return type declared by the interface LAG\AdminBundle\DataProv...tyLoaderInterface::load of type Traversable.
Loading history...
87
        }
88
    
89
        // load the entities into the Admin
90
        return $entities;
91
    }
92
    
93
    /**
94
     * Load entities using Pagerfanta.
95
     *
96
     * @param array $criteria
97
     * @param array $orderBy
98
     * @param int   $limit
99
     * @param int   $offset
100
     *
101
     * @return Pagerfanta
102
     */
103
    private function loadPaginate(array $criteria, array $orderBy, $limit, $offset)
104
    {
105
        // only pagerfanta adapter is yet supported
106
        if ('pagerfanta' !== $this->pagerName) {
107
            throw new LogicException(
108
                'Only pagerfanta value is allowed for pager parameter, given '.$this->pagerName
109
            );
110
        }
111
    
112
        // only load strategy multiple is allowed for pagination (ie, can not paginate if only one entity is loaded)
113
        if (AdminInterface::LOAD_STRATEGY_MULTIPLE !== $this->loadStrategy) {
114
            throw new LogicException(
115
                'Only "strategy_multiple" value is allowed for pager parameter, given '.$this->loadStrategy
116
            );
117
        }
118
        
119
        // adapter to pagerfanta
120
        $adapter = new PagerfantaAdminAdapter($this->dataProvider, $criteria, $orderBy);
121
        // create pager
122
        $pager = new Pagerfanta($adapter);
123
        $pager->setMaxPerPage($limit);
124
        $pager->setCurrentPage($offset);
125
        
126
        return $pager;
127
    }
128
    
129
    /**
130
     * Load entities using to configured data provider, without using a pagination system.
131
     *
132
     * @param array $criteria
133
     * @param array $orderBy
134
     *
135
     * @return array|Collection
136
     */
137
    private function loadWithoutPagination(array $criteria, $orderBy)
138
    {
139
        return $this
140
            ->dataProvider
141
            ->findBy($criteria, $orderBy, null, null)
142
        ;
143
    }
144
    
145
    /**
146
     * Return the associated DataProvider.
147
     *
148
     * @return DataProviderInterface
149
     */
150 1
    public function getDataProvider()
151
    {
152 1
        return $this->dataProvider;
153
    }
154
}
155