Passed
Push — master ( 896e22...61bd56 )
by Dāvis
03:36
created

DoctrineProvider::getQuery()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Sitemap\Provider;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\Query;
7
use Doctrine\ORM\QueryBuilder;
8
use Symfony\Component\Routing\RouterInterface;
9
use Sludio\HelperBundle\Sitemap\Sitemap;
10
11
class DoctrineProvider extends AbstractProvider
12
{
13
    protected $router;
14
    protected $em;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
15
16
    protected $options = [
17
        'entity' => null,
18
        'loc' => [],
19
        'query_method' => null,
20
        'lastmod' => null,
21
        'priority' => null,
22
        'changefreq' => null,
23
    ];
24
25
    /**
26
     * Constructor
27
     *
28
     * @param Entitymanager   $em      Doctrine entity manager.
29
     * @param RouterInterface $router  The application router.
30
     * @param array           $options The options (see the class comment).
31
     */
32
    public function __construct(EntityManager $em, RouterInterface $router, array $options)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
33
    {
34
        parent::__construct($router, $options);
35
36
        $this->em = $em;
37
    }
38
39
    /**
40
     * Populate a sitemap using a Doctrine entity.
41
     *
42
     * @param Sitemap $sitemap The current sitemap.
43
     */
44
    public function populate(Sitemap $sitemap)
45
    {
46
        $query = $this->getQuery($this->options['entity'], $this->options['query_method']);
47
        $results = $query->iterate();
48
49
        // and populate the sitemap!
50
        while (($result = $results->next()) !== false) {
51
            $sitemap->add($this->resultToUrl($result[0]));
52
53
            $this->em->detach($result[0]);
54
        }
55
    }
56
57
    protected function getQuery($entity, $method = null)
58
    {
59
        $repo = $this->em->getRepository($entity);
60
61
        if ($method !== null) {
62
            $query = $repo->$method();
63
        } else {
64
            $query = $repo->createQueryBuilder('o')->getQuery();
65
        }
66
67
        if ($query instanceof QueryBuilder) {
68
            $query = $query->getQuery();
69
        }
70
71
        if (!$query instanceof Query) {
72
            throw new \RuntimeException(sprintf('Expected instance of Query, got %s (see method %s:%s)', get_class($query), $entity, $method));
73
        }
74
75
        return $query;
76
    }
77
}