DbStaticProvider::checkRefsAreLoaded()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 2
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * @author    Rik van der Kemp <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\UrlBundle\Url;
7
8
use Doctrine\ORM\EntityManager;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
use Symfony\Component\Routing\RouterInterface;
13
use Zicht\Bundle\UrlBundle\Entity\StaticReference;
14
15
/**
16
 * Static provider holds a set of urls
17
 */
18
class DbStaticProvider implements Provider
19
{
20
    /**
21
     * @var EntityManager
22
     */
23
    private $em;
24
25
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
26
     * @var null|RequestStack $requestStack
27
     */
28
    private $requestStack;
29
30
    /**
31
     * Holds the static references
32
     *
33
     * @var array
34
     */
35
    private $refs = null;
36
37
    /**
38
     * The locale to use when all things fail
39
     *
40
     * @var null|string
41
     */
42
    private $fallback_locale = null;
43
44
    /**
45
     * Create the provider with a set of static references, i.e. mappings from name to url.
46
     *
47
     * @param EntityManager $em
48
     * @param RequestStack|null $requestStack
49 2
     */
50
    public function __construct(EntityManager $em, RequestStack $requestStack = null)
51 2
    {
52 2
        $this->em = $em;
53 2
        $this->requestStack = $requestStack;
54
    }
55
56
    /**
57
     * Add the array as references
58
     *
59
     * @return void
60 2
     */
61
    public function addAll()
62
    {
63 2
        // Make sure refs are not null any more, else it keeps checking on every static ref
64
        $this->refs = array();
65
66 2
        /** @var StaticReference $repos */
67
        $references = $this->em->getRepository('ZichtUrlBundle:StaticReference')->getAll($this->getLocale());
68 2
69 2
        foreach ($references as $static_reference) {
70 2
            foreach ($static_reference->getTranslations() as $translation) {
71
                $this->refs[$static_reference->getMachineName()][$translation->getLocale()] = $translation->getUrl();
72
            }
73 2
        }
74
    }
75
76
    /**
77
     * @{inheritDoc}
78 2
     */
79
    public function supports($object)
80 2
    {
81
        $this->checkRefsAreLoaded();
82 2
83
        return is_string($object) && isset($this->refs[$object][$this->getLocale()]);
84
    }
85
86
    /**
87
     * @{inheritDoc}
88 1
     */
89
    public function url($object, array $options = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
90 1
    {
91
        $this->checkRefsAreLoaded();
92 1
93
        $url = $this->refs[$object][$this->getLocale()];
94 1
95 1
        if (!preg_match('/^(http|https)/', $url) && (null !== ($request = $this->getMasterRequest()))) {
96
            $url = $request->getBaseUrl() . '/' . ltrim($url, '/');
97
        }
98 1
99
        return $url;
100
    }
101
102
103
    /**
104
     * Returns the locale parameter for the current request, if any.
105
     *
106
     * @return mixed
107 2
     */
108
    public function getLocale()
109 2
    {
110 1
        if (null !== ($request = $this->getMasterRequest())) {
111
            $locale = $request->get('_locale');
112
        }
113 2
114 1
        if (!isset($locale)) {
115
            $locale = $this->fallback_locale;
116
        }
117 2
118
        return $locale;
119
    }
120
121
    /**
122
     * Load all static references from the database
123
     *
124
     * @return void
125 2
     */
126
    private function checkRefsAreLoaded()
127 2
    {
128 2
        if (is_null($this->refs)) {
0 ignored issues
show
introduced by
The condition is_null($this->refs) is always false.
Loading history...
129
            $this->addAll();
130 2
        }
131
    }
132
133
    /**
134
     * @return null|Request
135 2
     */
136
    private function getMasterRequest()
137 2
    {
138
        return (!is_null($this->requestStack)) ? $this->requestStack->getMasterRequest() : null;
139
    }
140
141
    /**
142
     * Fallback locale to use whenever the reference for a specific locale is not set.
143
     *
144
     * @param string $locale
145
     * @return void
146
     */
147
    public function setFallbackLocale($locale)
148
    {
149
        $this->fallback_locale = $locale;
150
    }
151
}
152