Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

BusinessEntityCacheReader::findById()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
namespace Victoire\Bundle\BusinessEntityBundle\Reader;
4
5
use Victoire\Bundle\BusinessEntityBundle\Annotation\AnnotationDriver;
6
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity;
7
use Victoire\Bundle\CoreBundle\Cache\VictoireCache;
8
use Victoire\Bundle\WidgetBundle\Helper\WidgetHelper;
9
10
/**
11
 * The BusinessEntity Cache Reader.
12
 *
13
 * ref: victoire_business_entity.cache_reader
14
 */
15
class BusinessEntityCacheReader
16
{
17
    protected $cache;
18
    protected $widgetHelper;
19
    protected $driver; // @victoire_business_entity.annotation_driver
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param VictoireCache    $cache
25
     * @param WidgetHelper     $widgetHelper
26
     * @param AnnotationDriver $driver       If cache returns empty results, we try to refectch data
27
     */
28
    public function __construct(VictoireCache $cache, WidgetHelper $widgetHelper, AnnotationDriver $driver)
29
    {
30
        $this->cache = $cache;
31
        $this->widgetHelper = $widgetHelper;
32
        $this->driver = $driver;
33
    }
34
35
    /**
36
     * @param string $widgetName
37
     *
38
     * @return array
39
     */
40
    public function getReceiverProperties($widgetName)
41
    {
42
        $widgetMetadatas = $this->fetch('victoire_business_entity_widgets');
43
44
        if (isset($widgetMetadatas[$widgetName]) && array_key_exists('receiverProperties', $widgetMetadatas[$widgetName])) {
45
            return $widgetMetadatas[$widgetName]['receiverProperties'];
46
        }
47
48
        return [];
49
    }
50
51
    /**
52
     * Fetch in Cache system and try to reparse Annotation if no results.
53
     *
54
     * @param $key
55
     *
56
     * @throws \Doctrine\ORM\Mapping\MappingException
57
     *
58
     * @return mixed
59
     */
60
    protected function fetch($key)
61
    {
62
        $results = $this->cache->get($key, null);
63
64
        if (!$results) {
65
            //Reparse all entities to find some @VIC Annotation
66
            foreach ($this->driver->getAllClassNames() as $className) {
67
                $this->driver->parse(new \ReflectionClass($className));
68
            }
69
            $results = $this->cache->get($key, []);
70
        }
71
72
        return $results;
73
    }
74
}
75