Issues (3627)

bundles/ReportBundle/Generator/ReportGenerator.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\ReportBundle\Generator;
13
14
use Doctrine\DBAL\Connection;
15
use Mautic\ChannelBundle\Helper\ChannelListHelper;
16
use Mautic\ReportBundle\Entity\Report;
17
use Mautic\ReportBundle\Form\Type\ReportType;
18
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
19
use Symfony\Component\EventDispatcher\EventDispatcher;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\Form\FormFactoryInterface;
22
23
/**
24
 * Report generator.
25
 */
26
class ReportGenerator
27
{
28
    /**
29
     * @var Connection
30
     */
31
    private $db;
32
33
    /**
34
     * @var EventDispatcher
35
     */
36
    private $dispatcher;
37
38
    /**
39
     * @var \Symfony\Component\Form\FormFactoryInterface
40
     */
41
    private $formFactory;
42
43
    /**
44
     * @var \Mautic\ReportBundle\Entity\Report
45
     */
46
    private $entity;
47
48
    /**
49
     * @var string
50
     */
51
    private $validInterface = 'Mautic\\ReportBundle\\Builder\\ReportBuilderInterface';
52
53
    /**
54
     * @var string
55
     */
56
    private $contentTemplate;
57
58
    /**
59
     * @var ChannelListHelper
60
     */
61
    private $channelListHelper;
62
63
    /**
64
     * ReportGenerator constructor.
65
     */
66
    public function __construct(EventDispatcherInterface $dispatcher, Connection $db, Report $entity, ChannelListHelper $channelListHelper, FormFactoryInterface $formFactory = null)
67
    {
68
        $this->db                = $db;
69
        $this->dispatcher        = $dispatcher;
0 ignored issues
show
Documentation Bug introduced by
$dispatcher is of type Symfony\Component\EventD...ventDispatcherInterface, but the property $dispatcher was declared to be of type Symfony\Component\EventDispatcher\EventDispatcher. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
70
        $this->formFactory       = $formFactory;
71
        $this->channelListHelper = $channelListHelper;
72
        $this->entity            = $entity;
73
    }
74
75
    /**
76
     * Gets query.
77
     *
78
     * @param array $options Optional options array for the query
79
     *
80
     * @return \Doctrine\DBAL\Query\QueryBuilder
81
     */
82
    public function getQuery(array $options = [])
83
    {
84
        $builder = $this->getBuilder();
85
86
        $query = $builder->getQuery($options);
87
88
        $this->contentTemplate = $builder->getContentTemplate();
89
90
        return $query;
91
    }
92
93
    /**
94
     * Gets form.
95
     *
96
     * @param Report $entity  Report Entity
97
     * @param array  $options Parameters set by the caller
98
     *
99
     * @return \Symfony\Component\Form\FormInterface
100
     */
101
    public function getForm(Report $entity, $options)
102
    {
103
        return $this->formFactory->createBuilder(ReportType::class, $entity, $options)->getForm();
104
    }
105
106
    /**
107
     * Gets the getContentTemplate path.
108
     *
109
     * @return string
110
     */
111
    public function getContentTemplate()
112
    {
113
        return $this->contentTemplate;
114
    }
115
116
    /**
117
     * Gets report builder.
118
     *
119
     * @return \Mautic\ReportBundle\Builder\ReportBuilderInterface
120
     *
121
     * @throws \Symfony\Component\DependencyInjection\Exception\RuntimeException
122
     */
123
    protected function getBuilder()
124
    {
125
        $className = '\\Mautic\\ReportBundle\\Builder\\MauticReportBuilder';
126
127
        if (!class_exists($className)) {
128
            throw new RuntimeException('The MauticReportBuilder does not exist.');
129
        }
130
131
        $reflection = new \ReflectionClass($className);
132
133
        if (!$reflection->implementsInterface($this->validInterface)) {
134
            throw new RuntimeException(sprintf("ReportBuilders have to implement %s, and %s doesn't implement it", $this->validInterface, $className));
135
        }
136
137
        return $reflection->newInstanceArgs([$this->dispatcher, $this->db, $this->entity, $this->channelListHelper]);
138
    }
139
}
140