Completed
Push — master ( 829e7b...9495f2 )
by Nate
01:24
created

managers/OrganizationTypeRelationshipManager.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\managers;
10
11
use craft\helpers\ArrayHelper;
12
use craft\helpers\Json;
13
use flipbox\craft\ember\helpers\QueryHelper;
14
use flipbox\organizations\elements\Organization;
15
use flipbox\organizations\Organizations;
16
use flipbox\organizations\queries\OrganizationTypeAssociationQuery;
17
use flipbox\organizations\records\OrganizationType;
18
use flipbox\organizations\records\OrganizationTypeAssociation;
19
20
/**
21
 * Manages Organization Types associated to Organizations
22
 *
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 2.0.0
25
 *
26
 * @property OrganizationTypeAssociation[] $associations
27
 *
28
 * @method OrganizationTypeAssociation findOrCreate($object)
29
 * @method OrganizationTypeAssociation findOne($object = null)
30
 * @method OrganizationTypeAssociation findOrFail($object)
31
 */
32
class OrganizationTypeRelationshipManager implements RelationshipManagerInterface
33
{
34
    use RelationshipManagerTrait;
35
36
    /**
37
     * @var Organization
38
     */
39
    private $organization;
40
41
    /**
42
     * @param Organization $organization
43
     */
44
    public function __construct(Organization $organization)
45
    {
46
        $this->organization = $organization;
47
    }
48
49
    /**
50
     * @param array $criteria
51
     * @return OrganizationTypeAssociationQuery
52
     */
53
    protected function query(array $criteria = []): OrganizationTypeAssociationQuery
54
    {
55
        /** @noinspection PhpUndefinedMethodInspection */
56
        $query = OrganizationTypeAssociation::find()
57
            ->setOrganizationId($this->organization->getId() ?: false)
0 ignored issues
show
It seems like $this->organization->getId() ?: false can also be of type false; however, flipbox\organizations\qu...it::setOrganizationId() does only seem to accept string|array<integer,str...nts\Organization>>|null, did you maybe forget to handle an error condition?
Loading history...
58
            ->orderBy([
59
                'sortOrder' => SORT_ASC
60
            ]);
61
62
        if (!empty($criteria)) {
63
            QueryHelper::configure(
64
                $query,
65
                $criteria
66
            );
67
        }
68
69
        return $query;
70
    }
71
72
    /**
73
     * @param OrganizationTypeAssociation|OrganizationType|int|string $type
74
     * @return OrganizationTypeAssociation
75
     */
76
    protected function create($type): OrganizationTypeAssociation
77
    {
78
        return (new OrganizationTypeAssociation())
79
            ->setOrganization($this->organization)
80
            ->setType($this->resolveType($type));
81
    }
82
83
84
    /*******************************************
85
     * SAVE
86
     *******************************************/
87
88
    /**
89
     * @inheritDoc
90
     */
91
    protected function associationDelta(): array
92
    {
93
        $existingAssociations = $this->query()
94
            ->indexBy('typeId')
95
            ->all();
96
97
        $associations = [];
98
        $order = 1;
99
        foreach ($this->findAll() as $newAssociation) {
100
            if (null === ($association = ArrayHelper::remove(
101
                $existingAssociations,
102
                $newAssociation->getTypeId()
103
            ))) {
104
                $association = $newAssociation;
105
            }
106
107
            $association->sortOrder = $order++;
108
109
            $associations[] = $association;
110
        }
111
112
        return [$associations, $existingAssociations];
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    protected function handleAssociationError()
119
    {
120
        $this->organization->addError('types', 'Unable to save organization types.');
121
    }
122
123
124
    /*******************************************
125
     * UTILS
126
     *******************************************/
127
128
    /**
129
     * @param OrganizationTypeAssociation|OrganizationType|int|array|null $type
130
     * @return int|null
131
     */
132
    protected function findKey($type = null)
133
    {
134
        if (null === ($record = $this->resolveType($type))) {
135
            Organizations::info(sprintf(
136
                "Unable to resolve organization type: %s",
137
                (string)Json::encode($type)
138
            ));
139
            return null;
140
        }
141
142
        foreach ($this->findAll() as $key => $association) {
143
            if ($association->getTypeId() == $record->id) {
144
                return $key;
145
            }
146
        }
147
148
        return null;
149
    }
150
151
    /**
152
     * @param OrganizationTypeAssociation|OrganizationType|int|array|null $type
153
     * @return OrganizationType|null
154
     */
155
    protected function resolveType($type = null)
156
    {
157
        if (null === $type) {
158
            return null;
159
        }
160
161
        if ($type instanceof OrganizationTypeAssociation) {
162
            return $type->getType();
163
        }
164
165
        if ($type instanceof OrganizationType) {
166
            return $type;
167
        }
168
169
        if (is_array($type) &&
170
            null !== ($id = ArrayHelper::getValue($type, 'id'))
171
        ) {
172
            $type = ['id' => $id];
173
        }
174
175
        return OrganizationType::findOne($type);
176
    }
177
}
178