Passed
Pull Request — master (#365)
by Dmitry
21:01
created

SaleController::indexPyzAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 33
rs 9.568
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Pyz\Yves\ExampleProductSalePage\Controller;
9
10
use InvalidArgumentException;
11
use Pyz\Yves\ExampleProductSalePage\Plugin\Router\ExampleProductSaleRouteProviderPlugin;
12
use Spryker\Yves\Kernel\Controller\AbstractController;
13
use Spryker\Yves\Kernel\View\View;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
17
/**
18
 * @method \Pyz\Yves\ExampleProductSalePage\ExampleProductSalePageFactory getFactory()
19
 * @method \Pyz\Client\ExampleProductSalePage\ExampleProductSalePageClientInterface getClient()
20
 */
21
class SaleController extends AbstractController
22
{
23
    /**
24
     * @param string $categoryPath
25
     * @param \Symfony\Component\HttpFoundation\Request $request
26
     *
27
     * @return \Spryker\Yves\Kernel\View\View
28
     */
29
    public function indexAction($categoryPath, Request $request): View
30
    {
31
        $parameters = $request->query->all();
32
33
        $categoryNode = [];
34
        if ($categoryPath) {
35
            $categoryNode = $this->getCategoryNode($categoryPath);
36
37
            $parameters['category'] = $categoryNode['node_id'];
38
        }
39
40
        $searchResults = $this
41
            ->getClient()
42
            ->saleSearch($parameters);
43
44
        $searchResults['category'] = $categoryNode;
45
        $searchResults['filterPath'] = ExampleProductSaleRouteProviderPlugin::ROUTE_NAME_SALE;
46
        $searchResults['viewMode'] = $this->getFactory()
47
            ->getCatalogClient()
48
            ->getCatalogViewMode($request);
49
50
        $numberFormatConfigTransfer = $this->getFactory()
51
            ->getUtilNumberService()
52
            ->getNumberFormatConfig(
53
                $this->getFactory()->getLocaleClient()->getCurrentLocale(),
54
            );
55
56
        return $this->view(
57
            array_merge($searchResults, [
58
                'numberFormatConfig' => $numberFormatConfigTransfer->toArray(),
59
            ]),
60
            $this->getFactory()->getExampleProductSalePageWidgetPlugins(),
61
            '@ExampleProductSalePage/views/sale-example/sale-example.twig',
62
        );
63
    }
64
65
    /**
66
     * @param string $categoryPath
67
     *
68
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
69
     *
70
     * @return array<mixed>
71
     */
72
    protected function getCategoryNode($categoryPath): array
73
    {
74
        $defaultLocale = current($this->getFactory()->getStore()->getAvailableLocaleIsoCodes());
75
        $categoryPathPrefix = '/' . $this->getLanguageFromLocale($defaultLocale);
76
        $fullCategoryPath = $categoryPathPrefix . '/' . ltrim($categoryPath, '/');
77
78
        $categoryNode = $this->getFactory()
79
            ->getUrlStorageClient()
80
            ->matchUrl($fullCategoryPath, $this->getLocale());
81
82
        if (!$categoryNode || empty($categoryNode['data'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryNode of type array<string,mixed> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
83
            throw new NotFoundHttpException(sprintf(
84
                'Category not found by path %s (full path %s)',
85
                $categoryPath,
86
                $fullCategoryPath,
87
            ));
88
        }
89
90
        return $categoryNode['data'];
91
    }
92
93
    /**
94
     * @param string $locale
95
     *
96
     * @throws \InvalidArgumentException
97
     *
98
     * @return string
99
     */
100
    protected function getLanguageFromLocale(string $locale): string
101
    {
102
        $position = strpos($locale, '_');
103
        if ($position === false) {
104
            throw new InvalidArgumentException(sprintf('Invalid format for locale `%s`, expected `xx_YY`.', $locale));
105
        }
106
107
        return substr($locale, 0, $position);
108
    }
109
}
110