FindHighestPostSlugSuffixQuery::execute()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.2408
c 0
b 0
f 0
cc 5
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Core\Component\Blog\Application\Query\DQL;
16
17
use Acme\App\Core\Component\Blog\Application\Query\FindHighestPostSlugSuffixQueryInterface;
18
use Acme\App\Core\Component\Blog\Domain\Post\Post;
19
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
20
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
21
use Acme\App\Core\Port\Persistence\QueryServiceInterface;
22
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface;
23
24
final class FindHighestPostSlugSuffixQuery implements FindHighestPostSlugSuffixQueryInterface
25
{
26
    private const SLUG_ALIAS = 'slug';
27
28
    /**
29
     * @var DqlQueryBuilderInterface
30
     */
31
    private $dqlQueryBuilder;
32
33
    /**
34
     * @var QueryServiceInterface
35
     */
36
    private $queryService;
37
38
    public function __construct(
39
        DqlQueryBuilderInterface $dqlQueryBuilder,
40
        QueryServiceRouterInterface $queryService
41
    ) {
42
        $this->dqlQueryBuilder = $dqlQueryBuilder;
43
        $this->queryService = $queryService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $queryService of type object<Acme\App\Core\Por...ServiceRouterInterface> is incompatible with the declared type object<Acme\App\Core\Por...\QueryServiceInterface> of property $queryService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
    }
45
46
    public function execute(string $slug): int
47
    {
48
        $this->dqlQueryBuilder->create(Post::class, 'Post')
49
            ->select('Post.slug as ' . self::SLUG_ALIAS)
50
            ->where('Post.slug LIKE :slug')
51
            ->setParameter('slug', $slug . '%');
52
53
        $resultCollection = $this->queryService->query($this->dqlQueryBuilder->build());
0 ignored issues
show
Bug introduced by
The method query() does not seem to exist on object<Acme\App\Core\Por...\QueryServiceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
55
        if (\count($resultCollection) === 0) {
56
            throw new EmptyQueryResultException();
57
        }
58
59
        $max = -1;
60
        foreach ($resultCollection as $result) {
61
            preg_match("/$slug\-?(\d*)$/", $result[self::SLUG_ALIAS], $matches);
62
            $max = (isset($matches[1]) && (int) $matches[1] > $max)
63
                ? (int) $matches[1]
64
                : $max;
65
        }
66
67
        return $max;
68
    }
69
}
70