Issues (5)

src/Doctrine/PageExtension.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\PageBundle\Doctrine;
6
7
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
8
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
9
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
10
use ApiPlatform\Metadata\Operation;
11
use Doctrine\ORM\QueryBuilder;
12
use ProjetNormandie\PageBundle\ValueObject\PageStatus;
0 ignored issues
show
The type ProjetNormandie\PageBundle\ValueObject\PageStatus was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Intl\Locale;
14
use ProjetNormandie\PageBundle\Entity\Page;
15
16
final class PageExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
17
{
18
    /**
19
     * @param QueryBuilder                $queryBuilder
20
     * @param QueryNameGeneratorInterface $queryNameGenerator
21
     * @param string                      $resourceClass
22
     * @param Operation|null              $operation
23
     * @param array                       $context
24
     * @return void
25
     */
26
    public function applyToCollection(
27
        QueryBuilder $queryBuilder,
28
        QueryNameGeneratorInterface $queryNameGenerator,
29
        string $resourceClass,
30
        ?Operation $operation = null,
31
        array $context = []
32
    ): void {
33
        $this->addWhere($queryBuilder, $resourceClass);
34
    }
35
36
    /**
37
     * @param QueryBuilder                $queryBuilder
38
     * @param QueryNameGeneratorInterface $queryNameGenerator
39
     * @param string                      $resourceClass
40
     * @param array                       $identifiers
41
     * @param Operation|null              $operation
42
     * @param array                       $context
43
     * @return void
44
     */
45
    public function applyToItem(
46
        QueryBuilder $queryBuilder,
47
        QueryNameGeneratorInterface $queryNameGenerator,
48
        string $resourceClass,
49
        array $identifiers,
50
        ?Operation $operation = null,
51
        array $context = []
52
    ): void {
53
        $this->addWhere($queryBuilder, $resourceClass);
54
    }
55
56
    /**
57
     * @param QueryBuilder $queryBuilder
58
     * @param string       $resourceClass
59
     */
60
    private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void
61
    {
62
        if ($resourceClass != Page::class) {
63
            return;
64
        }
65
        $locale = Locale::getDefault();
66
        if (!in_array($locale, array('en', 'fr'))) {
67
            $locale = 'en';
68
        }
69
        $queryBuilder->leftJoin('o.translations', 't', 'WITH', "t.locale='$locale'")
70
            ->addSelect('t')
71
            ->andWhere('o.status = :status')
72
            ->setParameter('status', PageStatus::PUBLIC);
73
    }
74
}
75