1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Vehicle\App\Mutation; |
4
|
|
|
|
5
|
|
|
use App\Common\App\Transformer\AppGlobalId; |
6
|
|
|
use App\Vehicle\Domain\Truck; |
7
|
|
|
use App\Vehicle\Domain\VehicleRepositoryInterface; |
8
|
|
|
use GraphQL\Error\UserError; |
9
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface; |
10
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\MutationInterface; |
11
|
|
|
|
12
|
|
View Code Duplication |
class TruckMutation implements MutationInterface, AliasedInterface |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var VehicleRepositoryInterface |
16
|
|
|
*/ |
17
|
|
|
private $vehicleRepository; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param VehicleRepositoryInterface $vehicleRepository |
21
|
|
|
*/ |
22
|
|
|
public function __construct(VehicleRepositoryInterface $vehicleRepository) |
23
|
|
|
{ |
24
|
|
|
$this->vehicleRepository = $vehicleRepository; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $id |
29
|
|
|
* @param string $manufacturer |
30
|
|
|
* @param string $model |
31
|
|
|
* @param int $maximumLoad |
32
|
|
|
* @return Truck |
33
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
34
|
|
|
*/ |
35
|
|
|
public function createTruck(string $id, string $manufacturer, string $model, int $maximumLoad): Truck |
36
|
|
|
{ |
37
|
|
|
if ($truck = $this->vehicleRepository->find($id)) { |
|
|
|
|
38
|
|
|
throw new UserError(sprintf('Truck [%s] already exist', AppGlobalId::toGlobalId('Truck', $id))); |
39
|
|
|
|
40
|
|
|
return $truck; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->vehicleRepository->save( |
44
|
|
|
new Truck($id, $manufacturer, $model, $maximumLoad) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $id |
50
|
|
|
* @param string $manufacturer |
51
|
|
|
* @param string $model |
52
|
|
|
* @param int $maximumLoad |
53
|
|
|
* @return Truck |
54
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
55
|
|
|
*/ |
56
|
|
|
public function updateTruck(string $id, string $manufacturer, string $model, int $maximumLoad): Truck |
57
|
|
|
{ |
58
|
|
|
if (!$truck = $this->vehicleRepository->find($id)) { |
59
|
|
|
throw new UserError(sprintf('Truck [%s] not found', AppGlobalId::toGlobalId('Truck', $id))); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$truck |
63
|
|
|
->setManufacturer($manufacturer) |
64
|
|
|
->setModel($model) |
65
|
|
|
->setMaximumLoad($maximumLoad) |
66
|
|
|
; |
67
|
|
|
|
68
|
|
|
return $this->vehicleRepository->save($truck); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public static function getAliases(): array |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
'resolve' => 'TruckMutation', |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.