Completed
Push — master ( 0406c2...d58e57 )
by Christian
04:47
created

PiwikStatisticBlockService::getJavascripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ni-ju-san CMS.
5
 *
6
 * (c) Christian Gripp <[email protected]>
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 Core23\PiwikBundle\Block\Service;
13
14
use Core23\CoreBundle\Block\Service\BaseBlockService;
15
use Core23\PiwikBundle\Client\ClientFactory;
16
use Core23\PiwikBundle\Exception\PiwikException;
17
use Psr\Log\LoggerInterface;
18
use Sonata\AdminBundle\Form\FormMapper;
19
use Sonata\BlockBundle\Block\BlockContextInterface;
20
use Sonata\BlockBundle\Model\BlockInterface;
21
use Sonata\CoreBundle\Model\Metadata;
22
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
26
class PiwikStatisticBlockService extends BaseBlockService
27
{
28
    /**
29
     * @var LoggerInterface
30
     */
31
    protected $logger;
32
33
    /**
34
     * @var ClientFactory
35
     */
36
    protected $factory;
37
38
    /**
39
     * PiwikStatisticBlockService constructor.
40
     *
41
     * @param string          $name
42
     * @param EngineInterface $templating
43
     * @param ClientFactory   $factory
44
     * @param LoggerInterface $logger
45
     */
46
    public function __construct($name, EngineInterface $templating, ClientFactory $factory, LoggerInterface $logger)
47
    {
48
        parent::__construct($name, $templating);
49
50
        $this->factory = $factory;
51
        $this->logger    = $logger;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 View Code Duplication
    public function execute(BlockContextInterface $blockContext, Response $response = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        return $this->renderResponse($blockContext->getTemplate(), array(
60
            'context'  => $blockContext,
61
            'settings' => $blockContext->getSettings(),
62
            'block'    => $blockContext->getBlock(),
63
            'data'     => $this->getData($blockContext->getSettings()),
64
        ), $response);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
71
    {
72
        $formMapper->add('settings', 'sonata_type_immutable_array', array(
73
            'keys' => array(
74
                array('title', 'text', array(
75
                    'required' => false,
76
                    'label'    => 'form.label_title',
77
                )),
78
                array('host', 'text', array(
79
                    'required' => false,
80
                    'label'    => 'form.label_host',
81
                )),
82
                array('token', 'text', array(
83
                    'required' => false,
84
                    'label'    => 'form.label_token',
85
                )),
86
                array('site', 'number', array(
87
                    'label' => 'form.label_site',
88
                )),
89
                array('method', 'choice', array(
90
                    'choices' => array(
91
                        'VisitsSummary.getVisits'         => 'form.choice_visitors',
92
                        'VisitsSummary.getUniqueVisitors' => 'form.choice_unique_visitors',
93
                        'VisitsSummary.getActions '       => 'form.choice_hits',
94
                    ),
95
                    'label' => 'form.label_method',
96
                )),
97
                array('period', 'choice', array(
98
                    'choices' => array(
99
                        'day'   => 'form.choice_day',
100
                        'week'  => 'form.choice_week',
101
                        'month' => 'form.choice_month',
102
                        'year'  => 'form.choice_year',
103
                    ),
104
                    'label' => 'form.label_period',
105
                )),
106
                array('date', 'choice', array(
107
                    'choices' => array(
108
                        'last1'   => 'form.choice_today',
109
                        'last7'   => 'form.choice_1_week',
110
                        'last14'  => 'form.choice_2_weeks',
111
                        'last30'  => 'form.choice_1_month',
112
                        'last90'  => 'form.choice_3_months',
113
                        'last180' => 'form.choice_6_months',
114
                        'last360' => 'form.choice_1_year',
115
                    ),
116
                    'label' => 'form.label_date',
117
                )),
118
            ),
119
            'translation_domain' => 'Core23PiwikBundle',
120
        ));
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function configureSettings(OptionsResolver $resolver)
127
    {
128
        $resolver->setDefaults(array(
129
            'title'    => 'Piwik Statistic',
130
            'site'     => false,
131
            'method'   => 'VisitsSummary.getVisits',
132
            'host'     => null,
133
            'token'    => null,
134
            'period'   => 'day',
135
            'date'     => 'last30',
136
            'template' => 'Core23PiwikBundle:Block:block_piwik_statistic.html.twig',
137
        ));
138
139
        $resolver->setRequired(array('site', 'host', 'token'));
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getJavascripts($media)
146
    {
147
        return array(
148
            '/assets/js/chartist.js',
149
            '/assets/js/jquery.piwikTable.js',
150
        );
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function getStylesheets($media)
157
    {
158
        return array(
159
            '/assets/css/chartist.css',
160
        );
161
    }
162
163
    /**
164
     * @param array $settings
165
     *
166
     * @return mixed|null
167
     */
168
    protected function getData($settings = array())
169
    {
170
        try {
171
            $client = $this->factory->createPiwikClient($settings['host'], $settings['token']);
172
173
            $response = $client->call($settings['method'], array(
174
                'idSite' => $settings['site'],
175
                'period' => $settings['period'],
176
                'date'   => $settings['date'],
177
            ));
178
179
            return $response;
180
        } catch (PiwikException $ce) {
181
            $this->logger->warning('Error retrieving Piwik url: '.$settings['host']);
182
        }
183
184
        return;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function getBlockMetadata($code = null)
191
    {
192
        return new Metadata($this->getName(), (!is_null($code) ? $code : $this->getName()), false, 'Core23PiwikBundle', array(
193
            'class' => 'fa fa-area-chart',
194
        ));
195
    }
196
}
197