|
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; |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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
|
|
|
|
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..