MessageApiController::preSerializeEntity()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * @copyright   2016 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\ChannelBundle\Controller\Api;
13
14
use Mautic\ApiBundle\Controller\CommonApiController;
15
use Mautic\ChannelBundle\ChannelEvents;
16
use Mautic\ChannelBundle\Entity\Message;
17
use Mautic\ChannelBundle\Event\ChannelEvent;
18
use Symfony\Component\Form\Form;
19
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
20
21
/**
22
 * Class MessageController.
23
 */
24
class MessageApiController extends CommonApiController
25
{
26
    public function initialize(FilterControllerEvent $event)
27
    {
28
        $this->model            = $this->getModel('channel.message');
29
        $this->entityClass      = Message::class;
30
        $this->entityNameOne    = 'message';
31
        $this->entityNameMulti  = 'messages';
32
        $this->serializerGroups = ['messageDetails', 'messageChannelList', 'categoryList', 'publishDetails'];
33
34
        parent::initialize($event);
35
    }
36
37
    protected function prepareParametersFromRequest(Form $form, array &$params, $entity = null, $masks = [], $fields = [])
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

37
    protected function prepareParametersFromRequest(Form $form, array &$params, $entity = null, $masks = [], /** @scrutinizer ignore-unused */ $fields = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        parent::prepareParametersFromRequest($form, $params, $entity, $masks);
40
41
        if ('PATCH' != $this->request->getMethod()) {
42
            $channels = $this->getModel('channel.message')->getChannels();
0 ignored issues
show
Bug introduced by
The method getChannels() does not exist on Mautic\CoreBundle\Model\AbstractCommonModel. It seems like you code against a sub-type of Mautic\CoreBundle\Model\AbstractCommonModel such as Mautic\ChannelBundle\Model\MessageModel. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
            $channels = $this->getModel('channel.message')->/** @scrutinizer ignore-call */ getChannels();
Loading history...
43
            if (!isset($params['channels'])) {
44
                $params['channels'] = [];
45
            }
46
47
            foreach ($channels as $channelType => $channel) {
48
                if (!isset($params['channels'][$channelType])) {
49
                    $params['channels'][$channelType] = [
50
                        'isEnabled' => 0,
51
                        'channel'   => $channelType,
52
                    ];
53
                } else {
54
                    $params['channels'][$channelType]['channel']   = $channelType;
55
                    $params['channels'][$channelType]['isEnabled'] = (int) $params['channels'][$channelType]['isEnabled'];
56
                }
57
            }
58
        }
59
    }
60
61
    /**
62
     * Load and set channel names to the response.
63
     *
64
     * @param        $entity
65
     * @param string $action
66
     *
67
     * @return mixed
68
     */
69
    protected function preSerializeEntity(&$entity, $action = 'view')
70
    {
71
        $event = $this->dispatcher->dispatch(ChannelEvents::ADD_CHANNEL, new ChannelEvent());
72
73
        if ($channels = $entity->getChannels()) {
74
            foreach ($channels as $channel) {
75
                $repository = $event->getRepositoryName($channel->getChannel());
0 ignored issues
show
Bug introduced by
The method getRepositoryName() does not exist on Symfony\Component\EventDispatcher\Event. It seems like you code against a sub-type of Symfony\Component\EventDispatcher\Event such as Mautic\ChannelBundle\Event\ChannelEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
                /** @scrutinizer ignore-call */ 
76
                $repository = $event->getRepositoryName($channel->getChannel());
Loading history...
76
                $nameColumn = $event->getNameColumn($channel->getChannel());
0 ignored issues
show
Bug introduced by
The method getNameColumn() does not exist on Symfony\Component\EventDispatcher\Event. It seems like you code against a sub-type of Symfony\Component\EventDispatcher\Event such as Mautic\ChannelBundle\Event\ChannelEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
                /** @scrutinizer ignore-call */ 
77
                $nameColumn = $event->getNameColumn($channel->getChannel());
Loading history...
77
                $name       = $this->model->getChannelName($channel->getChannelId(), $repository, $nameColumn);
0 ignored issues
show
Bug introduced by
The method getChannelName() does not exist on Mautic\CoreBundle\Model\AbstractCommonModel. It seems like you code against a sub-type of Mautic\CoreBundle\Model\AbstractCommonModel such as Mautic\ChannelBundle\Model\MessageModel. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
                /** @scrutinizer ignore-call */ 
78
                $name       = $this->model->getChannelName($channel->getChannelId(), $repository, $nameColumn);
Loading history...
78
                $channel->setChannelName($name);
79
            }
80
        }
81
    }
82
}
83