1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Admin\Action; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
// From PSR-7 |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
// From 'charcoal-admin' |
12
|
|
|
use Charcoal\Admin\AdminAction; |
13
|
|
|
|
14
|
|
|
// From 'charcoal-core' |
15
|
|
|
use Charcoal\Loader\CollectionLoader; |
16
|
|
|
|
17
|
|
|
// From 'charcoal-attachment' |
18
|
|
|
use Charcoal\Attachment\Object\Attachment; |
19
|
|
|
use Charcoal\Attachment\Object\Join; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Disconnect two objects |
23
|
|
|
*/ |
24
|
|
|
class RemoveJoinAction extends AdminAction |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param RequestInterface $request A PSR-7 compatible Request instance. |
28
|
|
|
* @param ResponseInterface $response A PSR-7 compatible Response instance. |
29
|
|
|
* @return ResponseInterface |
30
|
|
|
*/ |
31
|
|
|
public function run(RequestInterface $request, ResponseInterface $response) |
32
|
|
|
{ |
33
|
|
|
$params = $request->getParams(); |
|
|
|
|
34
|
|
|
|
35
|
|
View Code Duplication |
if ( |
|
|
|
|
36
|
|
|
!isset($params['attachment_id']) || |
37
|
|
|
!isset($params['obj_id']) || |
38
|
|
|
!isset($params['obj_type']) || |
39
|
|
|
!isset($params['group']) |
40
|
|
|
) { |
41
|
|
|
$this->setSuccess(false); |
42
|
|
|
|
43
|
|
|
return $response; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$attachmentId = $params['attachment_id']; |
47
|
|
|
$objId = $params['obj_id']; |
48
|
|
|
$objType = $params['obj_type']; |
49
|
|
|
$group = $params['group']; |
50
|
|
|
|
51
|
|
|
// Try loading the object |
52
|
|
|
try { |
53
|
|
|
$obj = $this->modelFactory()->create($objType)->load($objId); |
|
|
|
|
54
|
|
|
} catch (Exception $e) { |
55
|
|
|
$this->setSuccess(false); |
56
|
|
|
|
57
|
|
|
return $response; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$joinProto = $this->modelFactory()->create(Join::class); |
61
|
|
|
if (!$joinProto->source()->tableExists()) { |
62
|
|
|
$joinProto->source()->createTable(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$loader = new CollectionLoader([ |
66
|
|
|
'logger' => $this->logger, |
67
|
|
|
'factory' => $this->modelFactory() |
68
|
|
|
]); |
69
|
|
|
$loader |
70
|
|
|
->setModel($joinProto) |
71
|
|
|
->addFilter('object_type', $objType) |
72
|
|
|
->addFilter('object_id', $objId) |
73
|
|
|
->addFilter('attachment_id', $attachmentId) |
74
|
|
|
->addFilter('group', $group); |
75
|
|
|
|
76
|
|
|
$existingJoins = $loader->load(); |
77
|
|
|
|
78
|
|
|
// Should be just one, tho. |
79
|
|
|
foreach ($existingJoins as $joinModel) { |
80
|
|
|
$joinModel->delete(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Try loading the attachment |
84
|
|
|
try { |
85
|
|
|
$attachment = $this->modelFactory()->create(Attachment::class)->load($attachmentId); |
86
|
|
|
if ($attachment['id'] !== null) { |
87
|
|
|
$attachment->delete(); |
88
|
|
|
} |
89
|
|
|
} catch (Exception $error) { |
90
|
|
|
$this->setSuccess(false); |
91
|
|
|
return $response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->setSuccess(true); |
95
|
|
|
|
96
|
|
|
return $response; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: