GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 19b66d...20da4a )
by
unknown
11:55 queued 12s
created

ProductSearchAction::getProducts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusReportPlugin\Controller\Action;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandler;
9
use FOS\RestBundle\View\ViewHandlerInterface;
10
use Sylius\Component\Core\Model\ProductInterface;
11
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
12
use Sylius\Component\Locale\Context\LocaleContextInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
final class ProductSearchAction
17
{
18
    /** @var ProductRepositoryInterface */
19
    private $productRepository;
20
21
    /** @var LocaleContextInterface */
22
    private $localeContext;
23
24
    /** @var ViewHandlerInterface */
25
    private $viewHandler;
26
27
    public function __construct(
28
        ProductRepositoryInterface $productRepository,
29
        LocaleContextInterface $localeContext,
30
        ViewHandler $viewHandler
31
    )
32
    {
33
        $this->productRepository = $productRepository;
34
        $this->localeContext = $localeContext;
35
        $this->viewHandler = $viewHandler;
36
    }
37
38
    public function __invoke(Request $request): Response
39
    {
40
        $locale = $this->localeContext->getLocaleCode();
41
42
        $products = $this->getProducts($request->get('name', ''), $locale);
43
        $view = View::create($products);
44
45
        $this->viewHandler->setExclusionStrategyGroups(['Autocomplete']);
46
        $view->getContext()->enableMaxDepth();
47
48
        return $this->viewHandler->handle($view);
49
    }
50
51
    private function getProducts($query, $locale): array
52
    {
53
        $products = [];
54
        $searchProducts = $this->productRepository->findByNamePart($query, $locale);
55
56
        /** @var ProductInterface $product */
57
        foreach ($searchProducts as $product) {
58
            $productLabel = ucfirst(strtolower($product->getName()));
59
            $isNew = count(array_filter($products, function ($product) use ($productLabel) {
60
                return $product['name'] === $productLabel;
61
            })) === 0;
62
63
            if ($isNew) {
64
                $products[] = [
65
                    'name' => $productLabel,
66
                    'id' => $product->getId(),
67
                ];
68
            }
69
        }
70
71
        return $products;
72
    }
73
}
74