Completed
Push — symfony3-fqcn-sylius-3 ( 1d8d47...2ae323 )
by Kamil
38:58 queued 18:19
created

findChannelByCodeOrFindFirst()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 6
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\AdminBundle\Controller;
13
14
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
15
use Sylius\Component\Core\Dashboard\DashboardStatisticsProviderInterface;
16
use Sylius\Component\Core\Model\ChannelInterface;
17
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\Routing\RouterInterface;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * @author Paweł Jędrzejewski <[email protected]>
26
 */
27
class DashboardController
28
{
29
    /**
30
     * @var DashboardStatisticsProviderInterface
31
     */
32
    private $statisticsProvider;
33
34
    /**
35
     * @var ChannelRepositoryInterface
36
     */
37
    private $channelRepository;
38
39
    /**
40
     * @var EngineInterface
41
     */
42
    private $templatingEngine;
43
44
    /**
45
     * @var RouterInterface
46
     */
47
    private $router;
48
49
    /**
50
     * @param DashboardStatisticsProviderInterface $statisticsProvider
51
     * @param ChannelRepositoryInterface $channelRepository
52
     * @param EngineInterface $templatingEngine
53
     * @param RouterInterface $router
54
     */
55
    public function __construct(
56
        DashboardStatisticsProviderInterface $statisticsProvider,
57
        ChannelRepositoryInterface $channelRepository,
58
        EngineInterface $templatingEngine,
59
        RouterInterface $router
60
    ) {
61
        $this->statisticsProvider = $statisticsProvider;
62
        $this->channelRepository = $channelRepository;
63
        $this->templatingEngine = $templatingEngine;
64
        $this->router = $router;
65
    }
66
67
    /**
68
     * @param Request $request
69
     *
70
     * @return Response
71
     */
72
    public function indexAction(Request $request)
73
    {
74
        $channelCode = $request->query->get('channelCode');
75
76
        /** @var ChannelInterface $channel */
77
        $channel = $this->findChannelByCodeOrFindFirst($channelCode);
78
79
        if (null === $channel) {
80
            return new RedirectResponse($this->router->generate('sylius_admin_channel_create'));
81
        }
82
83
        $statistics = $this->statisticsProvider->getStatisticsForChannel($channel);
84
85
        return $this->templatingEngine->renderResponse(
86
            'SyliusAdminBundle:Dashboard:index.html.twig',
87
            ['statistics' => $statistics, 'channel' => $channel]
88
        );
89
    }
90
91
    /**
92
     * @param string $channelCode
93
     *
94
     * @return ChannelInterface|null
95
     */
96
    private function findChannelByCodeOrFindFirst($channelCode)
97
    {
98
        $channel = null;
99
        if (null !== $channelCode) {
100
            $channel = $this->channelRepository->findOneByCode($channelCode);
101
        }
102
103
        if (null === $channel) {
104
            $channels = $this->channelRepository->findAll();
105
106
            $channel = current($channels) === false ? null : current($channels);
107
        }
108
109
        return $channel;
110
    }
111
}
112