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.

ProductSearchAction::getProducts()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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