-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDatabaseUpdaterTask.php
More file actions
100 lines (87 loc) · 3.12 KB
/
DatabaseUpdaterTask.php
File metadata and controls
100 lines (87 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/DoctrineProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\DoctrineProcessBundle\Task\Database;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\ProcessState;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Execute an update/delete in the database from a SQL statement.
*
* @see https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/data-retrieval-and-manipulation.html#list-of-parameters-conversion
*
* @phpstan-type Options array{
* 'sql': string,
* 'input_as_params': bool,
* 'params': array<string, mixed>,
* 'types': array<int<0, max>|string, ArrayParameterType|ParameterType|Type|string>
* }
*/
class DatabaseUpdaterTask extends AbstractConfigurableTask
{
public function __construct(
protected ManagerRegistry $doctrine,
protected LoggerInterface $logger,
) {
}
public function execute(ProcessState $state): void
{
$numberRows = $this->initializeStatement($state);
$state->setOutput($numberRows);
}
/**
* @return int the number of affected rows
*
* @throws Exception
*/
protected function initializeStatement(ProcessState $state): int
{
/** @var Options $options */
$options = $this->getOptions($state);
$connection = $this->getConnection($state);
$inputAsParams = $state->getInput();
$params = $options['input_as_params'] ? $inputAsParams : $options['params'];
if (!\is_array($params)) {
throw new \UnexpectedValueException('Expecting an array of params');
}
/** @var array<string, mixed> $params */
return (int) $connection->executeStatement($options['sql'], $params, $options['types']);
}
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['sql']);
$resolver->setAllowedTypes('sql', ['string']);
$resolver->setDefaults([
'connection' => null,
'input_as_params' => true,
'params' => [],
'types' => [],
]);
$resolver->setAllowedTypes('connection', ['null', 'string']);
$resolver->setAllowedTypes('input_as_params', ['bool']);
$resolver->setAllowedTypes('params', ['array']);
$resolver->setAllowedTypes('types', ['array']);
}
protected function getConnection(ProcessState $state): Connection
{
/** @var ?string $connectionOptions */
$connectionOptions = $this->getOption($state, 'connection');
/** @var Connection $connection */
$connection = $this->doctrine->getConnection($connectionOptions);
return $connection;
}
}