Completed
Push — master ( b9ed39...19bf11 )
by Craig
07:51 queued 01:43
created

ThemeEntityRepository::get()   C

Complexity

Conditions 10
Paths 160

Size

Total Lines 49
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 35
c 1
b 0
f 0
nc 160
nop 3
dl 0
loc 49
rs 5.2413

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\ThemeModule\Entity\Repository;
13
14
use Doctrine\ORM\EntityRepository;
15
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
16
use Zikula\ThemeModule\Entity\ThemeEntity;
17
18
class ThemeEntityRepository extends EntityRepository
19
{
20
    const STATE_ALL = 0;
21
    const STATE_ACTIVE = 1;
22
    const STATE_INACTIVE = 2;
23
24
    const TYPE_ALL = 0;
25
    const TYPE_XANTHIA3 = 3;
26
27
    const FILTER_ALL = 0;
28
    const FILTER_USER = 1;
29
    const FILTER_SYSTEM = 2;
30
    const FILTER_ADMIN = 3;
31
32
    /**
33
     * @var ZikulaHttpKernelInterface
34
     */
35
    private $kernel;
36
37
    /**
38
     * @param ZikulaHttpKernelInterface $kernel
39
     */
40
    public function setKernel(ZikulaHttpKernelInterface $kernel)
41
    {
42
        $this->kernel = $kernel;
43
    }
44
45
    /**
46
     * @return ZikulaHttpKernelInterface
47
     */
48
    private function getKernel()
49
    {
50
        if (!$this->kernel instanceof ZikulaHttpKernelInterface) {
51
            // no need to translate this message as it will only be seen by developers.
52
            $message = 'The "kernel" attribute is NULL. '
53
                . 'Did you retrieved this repository using `$doctrine->getRepository()`? '
54
                . 'If so, retrieve it instead directly from the container';
55
            throw new \LogicException($message);
56
        }
57
58
        return $this->kernel;
59
    }
60
61
    public function get($filter = self::FILTER_ALL, $state = self::STATE_ACTIVE, $type = self::TYPE_ALL)
62
    {
63
        $qb = $this->getEntityManager()->createQueryBuilder()
64
            ->select('t')
65
            ->from('ZikulaThemeModule:ThemeEntity', 't');
66
67
        if ($state != self::STATE_ALL) {
68
            $qb->andWhere('t.state = :state')
69
                ->setParameter('state', $state);
70
        }
71
        if ($type != self::TYPE_ALL) {
72
            $qb->andWhere('t.type = :type')
73
                ->setParameter('type', $type);
74
        }
75
        switch ($filter) {
76
            case self::FILTER_USER:
77
                $qb->andWhere('t.user = 1');
78
                break;
79
            case self::FILTER_SYSTEM:
80
                $qb->andWhere('t.system = 1');
81
                break;
82
            case self::FILTER_ADMIN:
83
                $qb->andWhere('t.admin = 1');
84
                break;
85
        }
86
87
        $qb->orderBy('t.displayname', 'ASC');
88
        $query = $qb->getQuery();
89
90
        /** @var $result ThemeEntity[] */
91
        $result = $query->getResult();
92
        $themesArray = [];
93
        foreach ($result as $theme) {
94
            $themesArray[$theme->getName()]= $theme->toArray();
95
            $kernel = $this->getKernel(); // allow to throw exception outside the try/catch block
96
            try {
97
                $themeBundle = $kernel->getTheme($theme['name']);
98
            } catch (\Exception $e) {
99
                $themeBundle = null;
100
            }
101
            $themesArray[$theme['name']]['vars'] = isset($themeBundle) ? $themeBundle->getThemeVars() : false;
102
        }
103
104
        if (!$themesArray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $themesArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
            return false;
106
        }
107
108
        return $themesArray;
109
    }
110
111
    public function removeAndFlush($entity)
112
    {
113
        $this->_em->remove($entity);
114
        $this->_em->flush();
115
    }
116
117
    public function persistAndFlush($entity)
118
    {
119
        $this->_em->persist($entity);
120
        $this->_em->flush();
121
    }
122
}
123