Completed
Push — master ( ec4c85...89b5b4 )
by Thibaud
04:36
created

PhraseanetExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 11
c 8
b 0
f 0
lcom 1
cbo 10
dl 0
loc 101
ccs 0
cts 56
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFunctions() 0 11 1
A fetchThumbnail() 0 6 1
A fetchThumbnailUrl() 0 10 2
A getRecordHash() 0 4 1
A getRecordCaption() 0 6 1
A getStoryCaption() 0 6 1
A getRecordMultiCaption() 0 6 1
A entryContainsPdfDocuments() 0 6 1
A getName() 0 4 1
1
<?php
2
3
namespace Alchemy\PhraseanetBundle\Twig;
4
5
use Alchemy\Phraseanet\Helper\InstanceHelperRegistry;
6
use PhraseanetSDK\Entity\FeedEntry;
7
use PhraseanetSDK\Entity\Record;
8
use PhraseanetSDK\Entity\Story;
9
10
class PhraseanetExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var InstanceHelperRegistry
14
     */
15
    private $helpers;
16
17
    public function __construct(InstanceHelperRegistry $helpers)
18
    {
19
        $this->helpers = $helpers;
20
    }
21
22
    public function getFunctions()
23
    {
24
        return array(
25
            new \Twig_SimpleFunction('record_hash', [ $this, 'getRecordHash' ]),
26
            new \Twig_SimpleFunction('record_caption', [$this, 'getRecordCaption']),
27
            new \Twig_SimpleFunction('story_caption', [$this, 'getStoryCaption']),
28
            new \Twig_SimpleFunction('fetch_thumbnail', [$this, 'fetchThumbnail']),
29
            new \Twig_SimpleFunction('fetch_thumbnail_url', [$this, 'fetchThumbnailUrl']),
30
            new \Twig_SimpleFunction('feed_entry_has_pdf_documents', [$this, 'entryContainsPdfDocuments'])
31
        );
32
    }
33
34
    public function fetchThumbnail($record, $thumbType = 'medium', $instanceName = null)
35
    {
36
        $thumbFetcher = $this->helpers->getHelper($instanceName)->getThumbHelper();
37
38
        return $thumbFetcher->fetch($record, $thumbType);
39
    }
40
41
    public function fetchThumbnailUrl($record, $thumbType = 'media', $instanceName = null)
42
    {
43
        $subdef = $this->fetchThumbnail($record, $thumbType, $instanceName);
44
45
        if (! $subdef) {
46
            return '';
47
        }
48
49
        return $subdef->getPermalink()->getUrl();
50
    }
51
52
    public function getRecordHash(Record $record, $instanceName = null)
53
    {
54
        return base64_encode(sprintf('%s_%s_%s', $instanceName, $record->getDataboxId(), $record->getRecordId()));
55
    }
56
57
    /**
58
     * @param Record $record
59
     * @param $field
60
     * @param null $locale
61
     * @param null $instanceName
62
     * @return null|string
63
     */
64
    public function getRecordCaption(Record $record, $field, $locale = null, $instanceName = null)
65
    {
66
        $metadataHelper = $this->helpers->getHelper($instanceName)->getMetadataHelper();
67
68
        return $metadataHelper->getRecordField($record, $field, $locale);
69
    }
70
71
    /**
72
     * @param Story $story
73
     * @param $field
74
     * @param null $locale
75
     * @param null $instanceName
76
     * @return string
77
     */
78
    public function getStoryCaption(Story $story, $field, $locale = null, $instanceName = null)
79
    {
80
        $metadataHelper = $this->helpers->getHelper($instanceName)->getMetadataHelper();
81
82
        return $metadataHelper->getStoryField($story, $field, $locale);
83
    }
84
85
    /**
86
     * @param Record $record
87
     * @param $field
88
     * @param null $locale
89
     * @param null $instanceName
90
     * @return array
91
     */
92
    public function getRecordMultiCaption(Record $record, $field, $locale = null, $instanceName = null)
93
    {
94
        $metadataHelper = $this->helpers->getHelper($instanceName)->getMetadataHelper();
95
96
        return $metadataHelper->getRecordMultiField($record, $field, $locale);
97
    }
98
99
    public function entryContainsPdfDocuments(FeedEntry $feedEntry, $name = null)
100
    {
101
        $feedHelper = $this->helpers->getHelper($name)->getFeedHelper();
102
103
        return $feedHelper->entryContainsPdfDocuments($feedEntry);
104
    }
105
106
    public function getName()
107
    {
108
        return 'phraseanet';
109
    }
110
}
111