Completed
Pull Request — master (#90)
by Arnaud
17:45
created

EntityLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 4
crap 6
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
    private $limit = 0;
50
    
51
    /**
52
     * EntityLoader constructor.
53
     *
54
     * @param DataProviderInterface $dataProvider
55
     */
56 1
    public function __construct(DataProviderInterface $dataProvider)
57
    {
58 1
        $this->dataProvider = $dataProvider;
59 1
    }
60
    
61
    /**
62
     * @param ActionConfiguration $configuration
63
     */
64
    public function configure(ActionConfiguration $configuration)
65
    {
66
        $this->loadStrategy = $configuration->getParameter('load_strategy');
67
        $this->isPaginationRequired = AdminInterface::LOAD_STRATEGY_MULTIPLE !== $this->loadStrategy;
68
        $this->pagerName = $configuration->getParameter('pager');
69
        $this->limit = $configuration->getParameter('max_per_page');
70
    }
71
    
72
    /***
73
     * @param array $criteria
74
     * @param array $orderBy
75
     * @param int   $limit
76
     * @param int   $offset
77
     *
78
     * @return array|Collection|Pagerfanta
79
     */
80
    public function load(array $criteria, array $orderBy = [], $limit = 25, $offset = 1)
81
    {
82
        if (false === $this->isPaginationRequired) {
83
            // load entities from the DataProvider using a pagination system
84
            $entities = $this->loadPaginate($criteria, $orderBy, $limit, $offset);
85
        }
86
        else {
87
            // if no pagination is required (edit action for example)
88
            $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 92 which is incompatible with the return type declared by the interface LAG\AdminBundle\DataProv...tyLoaderInterface::load of type Traversable.
Loading history...
89
        }
90
    
91
        // load the entities into the Admin
92
        return $entities;
93
    }
94
    
95
    /**
96
     * Load entities using Pagerfanta.
97
     *
98
     * @param array $criteria
99
     * @param array $orderBy
100
     * @param int   $limit
101
     * @param int   $offset
102
     *
103
     * @return Pagerfanta
104
     */
105
    private function loadPaginate(array $criteria, array $orderBy, $limit, $offset)
106
    {
107
        // only pagerfanta adapter is yet supported
108
        if ('pagerfanta' !== $this->pagerName) {
109
            throw new LogicException(
110
                'Only pagerfanta value is allowed for pager parameter, given '.$this->pagerName
111
            );
112
        }
113
    
114
        // only load strategy multiple is allowed for pagination (ie, can not paginate if only one entity is loaded)
115
        if (AdminInterface::LOAD_STRATEGY_MULTIPLE !== $this->loadStrategy) {
116
            throw new LogicException(
117
                'Only "strategy_multiple" value is allowed for pager parameter, given '.$this->loadStrategy
118
            );
119
        }
120
    
121
        if (null === $limit) {
122
            $limit = $this->limit;
123
        }
124
    
125
        if (null === $offset) {
126
            $offset = 1;
127
        }
128
    
129
        // adapter to pagerfanta
130
        $adapter = new PagerfantaAdminAdapter($this->dataProvider, $criteria, $orderBy);
131
        // create pager
132
        $pager = new Pagerfanta($adapter);
133
        $pager->setCurrentPage($offset);
134
        $pager->setMaxPerPage($limit);
135
    
136
        //dump($pager->getCurrentPageResults()[0]->getTitle());
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
        
138
        return $pager;
139
    }
140
    
141
    /**
142
     * Load entities using to configured data provider, without using a pagination system.
143
     *
144
     * @param array $criteria
145
     * @param array $orderBy
146
     *
147
     * @return array|Collection
148
     */
149
    private function loadWithoutPagination(array $criteria, $orderBy)
150
    {
151
        return $this
152
            ->dataProvider
153
            ->findBy($criteria, $orderBy, null, null)
154
        ;
155
    }
156
    
157
    /**
158
     * Return the associated DataProvider.
159
     *
160
     * @return DataProviderInterface
161
     */
162 1
    public function getDataProvider()
163
    {
164 1
        return $this->dataProvider;
165
    }
166
}
167