1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Action\Collection; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Exception\ValidationException; |
15
|
|
|
use Cake\ORM\Entity; |
16
|
|
|
use Cake\Utility\Hash; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class DeleteAction, uses POST and an array of entity ids to delete |
20
|
|
|
* |
21
|
|
|
* @package CakeDC\Api\Service\Action\Collection |
22
|
|
|
*/ |
23
|
|
|
class DeleteAction extends CollectionAction |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
2 |
|
public function validates() |
30
|
|
|
{ |
31
|
2 |
|
$datas = $this->data(); |
32
|
2 |
|
$this->_validateDataIsArray($datas); |
33
|
2 |
|
$index = 0; |
34
|
2 |
|
$pkKey = $this->getTable()->getPrimaryKey(); |
35
|
|
|
$errors = collection($datas)->reduce(function ($errors, $data) use ($pkKey, &$index) { |
36
|
2 |
|
$error = null; |
37
|
2 |
|
if (empty(Hash::get($data, $pkKey))) { |
38
|
|
|
$error = [ |
39
|
1 |
|
$pkKey => ['_empty' => 'Missing id'], |
40
|
1 |
|
]; |
41
|
1 |
|
} |
42
|
2 |
|
if ($error) { |
43
|
1 |
|
$errors[$index] = $error; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
2 |
|
$index++; |
47
|
|
|
|
48
|
2 |
|
return $errors; |
49
|
2 |
|
}, []); |
50
|
|
|
|
51
|
2 |
|
if (!empty($errors)) { |
52
|
1 |
|
throw new ValidationException(__('Validation failed, some keys missing for delete action'), 0, null, $errors); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Execute action. Returns the array of deleted id's |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
5 |
|
public function execute() |
64
|
|
|
{ |
65
|
5 |
|
$entities = $this->_newEntities([ |
66
|
5 |
|
'accessibleFields' => [$this->getTable()->getPrimaryKey() => true |
67
|
5 |
|
]]); |
68
|
|
|
|
69
|
2 |
|
return $this->_deleteMany($entities); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Delete many entities, atomic |
74
|
|
|
* |
75
|
|
|
* @param array $entities entities |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
2 |
|
protected function _deleteMany($entities) |
79
|
|
|
{ |
80
|
2 |
|
$deleted = []; |
81
|
2 |
|
$this->getTable()->getConnection()->transactional(function () use ($entities, &$deleted) { |
82
|
2 |
|
$errors = []; |
83
|
2 |
|
foreach ($entities as $index => $entity) { |
84
|
|
|
/** |
85
|
|
|
* @var Entity $entity |
86
|
|
|
*/ |
87
|
2 |
|
$entity->isNew(false); |
88
|
|
|
try { |
89
|
2 |
|
$this->getTable()->deleteOrFail($entity, ['atomic' => false]); |
90
|
2 |
|
} catch (\InvalidArgumentException $ex) { |
91
|
1 |
|
$errors[$index] = [ |
92
|
1 |
|
$entity->id => $ex->getMessage() |
93
|
1 |
|
]; |
94
|
|
|
} |
95
|
2 |
|
$deleted[] = $entity->get($this->getTable()->getPrimaryKey()); |
|
|
|
|
96
|
2 |
|
} |
97
|
|
|
|
98
|
2 |
|
if ($errors) { |
|
|
|
|
99
|
1 |
|
throw new ValidationException(__('Validation failed'), 0, null, $errors); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return true; |
103
|
2 |
|
}); |
104
|
|
|
|
105
|
1 |
|
return $deleted; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.