1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\hubspot\actions\objects; |
10
|
|
|
|
11
|
|
|
use craft\base\ElementInterface; |
12
|
|
|
use flipbox\ember\actions\model\traits\Manage; |
13
|
|
|
use flipbox\ember\exceptions\RecordNotFoundException; |
14
|
|
|
use flipbox\hubspot\fields\Objects; |
15
|
|
|
use flipbox\hubspot\HubSpot; |
16
|
|
|
use flipbox\hubspot\records\ObjectAssociation; |
17
|
|
|
use yii\base\Action; |
18
|
|
|
use yii\base\Model; |
19
|
|
|
use yii\web\HttpException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Flipbox Factory <[email protected]> |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractAssociationAction extends Action |
26
|
|
|
{ |
27
|
|
|
use Manage; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $field |
31
|
|
|
* @return Objects |
32
|
|
|
* @throws HttpException |
33
|
|
|
*/ |
34
|
|
|
protected function resolveField(string $field): Objects |
35
|
|
|
{ |
36
|
|
|
if (null === ($resourcesField = HubSpot::getInstance()->getObjectsField()->findById($field))) { |
37
|
|
|
return $this->handleInvalidFieldResponse($field); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $resourcesField; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Model $model |
45
|
|
|
* @return bool |
46
|
|
|
* @throws RecordNotFoundException |
47
|
|
|
*/ |
48
|
|
|
protected function ensureAssociation(Model $model): bool |
49
|
|
|
{ |
50
|
|
|
if (!$model instanceof ObjectAssociation) { |
51
|
|
|
throw new RecordNotFoundException(sprintf( |
52
|
|
|
"HubSpot Resource Association must be an instance of '%s', '%s' given.", |
53
|
|
|
ObjectAssociation::class, |
54
|
|
|
get_class($model) |
55
|
|
|
)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $fieldId |
63
|
|
|
* @throws HttpException |
64
|
|
|
*/ |
65
|
|
|
protected function handleInvalidFieldResponse(int $fieldId) |
66
|
|
|
{ |
67
|
|
|
throw new HttpException(sprintf( |
68
|
|
|
"The provided field '%s' must be an instance of '%s'", |
69
|
|
|
(string)$fieldId, |
70
|
|
|
(string)Objects::class |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $elementId |
76
|
|
|
* @throws HttpException |
77
|
|
|
*/ |
78
|
|
|
protected function handleInvalidElementResponse(int $elementId) |
79
|
|
|
{ |
80
|
|
|
throw new HttpException(sprintf( |
81
|
|
|
"The provided source '%s' must be an instance of '%s'", |
82
|
|
|
(string)$elementId, |
83
|
|
|
(string)ElementInterface::class |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|