Completed
Push — develop ( 465be7...e649e8 )
by
unknown
15:38 queued 07:53
created

OrganizationReference::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Organizations\Entity;
12
13
use Auth\Entity\UserInterface;
14
use Core\Entity\EntityInterface;
15
use Core\Entity\PermissionsInterface;
16
use DateTime;
17
use Doctrine\Common\Collections\Collection;
18
use Organizations\Repository\Organization as OrganizationRepository;
19
use Zend\Hydrator\HydratorInterface;
20
21
22
/**
23
 * Manages reference to an organization.
24
 *
25
 * As OrganizationInterface is also implemented (and all methods are proxied to the "real" organization
26
 * object), this class can be used as an organization.
27
 *
28
 * @author Mathias Gelhausen <[email protected]>
29
 * @todo update test
30
 */
31
class OrganizationReference implements
0 ignored issues
show
Coding Style introduced by
The property $_userId is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $_repository is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $_organization is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $_type is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
32
    OrganizationInterface,
33
    OrganizationReferenceInterface
34
{
35
    /*
36
     * Note: We start property names with an underscore to prevent
37
     * name collisions with the OrganizationInterface.
38
     */
39
40
    /**
41
     * The user id of the parent document.
42
     *
43
     * @var string
44
     */
45
    protected $_userId;
0 ignored issues
show
Coding Style introduced by
$_userId does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
46
47
    /**
48
     * The organization repository.
49
     *
50
     * @var \Organizations\Repository\Organization
51
     */
52
    protected $_repository;
0 ignored issues
show
Coding Style introduced by
$_repository does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
53
54
    /**
55
     * The organization
56
     *
57
     * @var Organization
58
     */
59
    protected $_organization;
0 ignored issues
show
Coding Style introduced by
$_organization does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
60
61
    /**
62
     * Reference type.
63
     *
64
     * @internal
65
     *      Initial value is null, so we can determine in {@link load()} if it has already run once.
66
     *
67
     * @var string
68
     */
69
    protected $_type;
0 ignored issues
show
Coding Style introduced by
$_type does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
70
71
    /**
72
     * Creates an instance.
73
     *
74
     * @param string                 $userId
75
     * @param OrganizationRepository $repository
76
     */
77
    public function __construct($userId, OrganizationRepository $repository)
78
    {
79
        $this->_userId = $userId;
80
        $this->_repository = $repository;
81
    }
82
83
    public function isOwner()
84
    {
85
        $this->load();
86
87
        return self::TYPE_OWNER == $this->_type;
88
    }
89
90
    public function isEmployee()
91
    {
92
        $this->load();
93
94
        return self::TYPE_EMPLOYEE == $this->_type;
95
    }
96
97
    public function hasAssociation()
98
    {
99
        $this->load();
100
101
        return self::TYPE_NONE != $this->_type;
102
    }
103
104
    public function getOrganization()
105
    {
106
        $this->load();
107
108
        return $this->_organization;
109
    }
110
111
    /**
112
     * Loads the organization from the database and sets the reference type.
113
     */
114
    protected function load()
115
    {
116
        if (null !== $this->_type) {
117
            return;
118
        }
119
120
        // Is the user the owner of the referenced organization?
121
        $org = $this->_repository->findByUser($this->_userId);
122
123
        if ($org) {
124
            $this->_type = self::TYPE_OWNER;
125
            $this->_organization = $org;
0 ignored issues
show
Documentation Bug introduced by
It seems like $org can also be of type array. However, the property $_organization is declared as type object<Organizations\Entity\Organization>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
126
127
            return;
128
        }
129
130
        // Is the user employed by the referenced organization?
131
        $org = $this->_repository->findByEmployee($this->_userId);
132
133
        if ($org) {
134
            $this->_type = self::TYPE_EMPLOYEE;
135
            $this->_organization = $org;
136
137
            return;
138
        }
139
140
        // It seems the user is not associated with an organization.
141
        $this->_type = self::TYPE_NONE;
142
    }
143
144
145
    /**
146
     * Executes a proxy call to the associated organization entity.
147
     *
148
     * Does nothing, if no organization is available.
149
     *
150
     * Call it like:
151
     * <pre>
152
     *   $this->proxy($method[, $arg1[, $arg2[, ...]]]);
153
     * </pre>
154
     *
155
     * @param $method
156
     *
157
     * @return self|mixed
158
     */
159
    protected function proxy($method)
160
    {
161
        if (!$this->hasAssociation()) {
162
            return $this;
163
        }
164
165
        $args = array_slice(func_get_args(), 1);
166
167
        $return = call_user_func_array(array($this->_organization, $method), $args);
168
169
        return ($return === $this->_organization) ? $this : $return;
170
    }
171
172
    /*
173
     * We need to implement each method of OrganizationInterface because we want to proxy
174
     * to a concrete instance of Organization and therefor cannot simply extend.
175
     */
176
177
    /**#@+
178
     * Proxies to the concrete Organization instance.
179
     *
180
     * {@inheritDoc}
181
     */
182
183
184
    public function __get($property)
185
    {
186
        return $this->proxy('__get', $property);
187
    }
188
189
    public function __set($property, $value)
190
    {
191
        return $this->proxy('__set', $property, $value);
192
    }
193
194
    public function __isset($property)
195
    {
196
        return $this->proxy('__isset', $property);
197
    }
198
199
    public function notEmpty($property, array $args=[])
200
    {
201
        return $this->proxy('notEmpty', $args);
202
    }
203
204
    public function hasProperty($property, $mode = self::PROPERTY_STRICT)
205
    {
206
        return $this->proxy('hasProperty', $mode);
207
    }
208
209
    public function setHydrator(HydratorInterface $hydrator)
210
    {
211
        return $this->proxy('setHydrator', $hydrator);
212
    }
213
214
    public function getHydrator()
215
    {
216
        return $this->proxy('getHydrator');
217
    }
218
219
    public function setId($id)
220
    {
221
        return $this->proxy('setId', $id);
222
    }
223
224
    public function getId()
225
    {
226
        return $this->proxy('getId');
227
    }
228
229
    public function setDateCreated(DateTime $date)
230
    {
231
        return $this->proxy('setDateCreated', $date);
232
    }
233
234
    public function getDateCreated()
235
    {
236
        return $this->proxy('getDateCreated');
237
    }
238
239
    public function setDateModified($date)
240
    {
241
        return $this->proxy('setDateModified', $date);
242
    }
243
244
    public function getDateModified()
245
    {
246
        return $this->proxy('getDateModified');
247
    }
248
249
    public function setParent(OrganizationInterface $parent)
250
    {
251
        return $this->proxy('setParent', $parent);
252
    }
253
254
    public function getParent()
255
    {
256
        return $this->proxy('getParent');
257
    }
258
259
    public function setContact(EntityInterface $contact = null)
260
    {
261
        return $this->proxy('setContact', $contact);
262
    }
263
264
    public function getContact()
265
    {
266
        return $this->proxy('getContact');
267
    }
268
269
    public function isHiringOrganization()
270
    {
271
        return $this->proxy('isHiringOrganization');
272
    }
273
274
    public function getHiringOrganizations()
275
    {
276
        return $this->proxy('getHiringOrganizations');
277
    }
278
279
    public function setImage(OrganizationImage $image)
280
    {
281
        return $this->proxy('setImage', $image);
282
    }
283
284
    public function getImage()
285
    {
286
        return $this->proxy('getImage');
287
    }
288
289
    public function setOrganizationName(OrganizationName $organizationNames)
290
    {
291
        return $this->proxy('setOrganizationName', $organizationNames);
292
    }
293
294
    public function getOrganizationName()
295
    {
296
        return $this->proxy('getOrganizationName');
297
    }
298
299
    public function getDescription()
300
    {
301
        return $this->proxy('getDescription');
302
    }
303
304
    public function setDescription($description)
305
    {
306
        return $this->proxy('setDescription', $description);
307
    }
308
309
    public function setEmployees(Collection $employees)
310
    {
311
        return $this->proxy('setEmployees', $employees);
312
    }
313
314
    public function getEmployees()
315
    {
316
        return $this->proxy('getEmployees');
317
    }
318
319
    public function getEmployeesByRole($role)
320
    {
321
        return $this->proxy('getEmployeesByRole');
322
    }
323
324
    public function getEmployee($userOrId)
325
    {
326
        return $this->proxy('getEmployee', $userOrId);
327
    }
328
329
    public function setExternalId($externalId)
330
    {
331
        return $this->proxy('setExternalId', $externalId);
332
    }
333
334
    public function getExternalId()
335
    {
336
        return $this->proxy('getExternalId');
337
    }
338
339
    public function getUser()
340
    {
341
        return $this->proxy('getUser');
342
    }
343
344
    public function setUser(UserInterface $user)
345
    {
346
        $this->_type = null; // force reload of references!
347
        return $this->proxy('setUser', $user);
348
    }
349
350
    public function getJobs()
351
    {
352
        return $this->proxy('getJobs');
353
    }
354
355
    public function getPermissions()
356
    {
357
        return $this->proxy('getPermissions');
358
    }
359
360
    public function setPermissions(PermissionsInterface $permissions)
361
    {
362
        return $this->proxy('setPermissions', $permissions);
363
    }
364
365
    public function getPermissionsResourceId()
366
    {
367
        return $this->proxy('getPermissionsResourceId');
368
    }
369
370
    public function getPermissionsUserIds($type = null)
371
    {
372
        return $this->proxy('getPermissionsUSerIds', $type);
373
    }
374
375
    public function getSearchableProperties()
376
    {
377
        return $this->proxy('getSearchableProperties');
378
    }
379
380
    public function setKeywords(array $keywords)
381
    {
382
        return $this->proxy('setKeywords', $keywords);
383
    }
384
385
    public function clearKeywords()
386
    {
387
        return $this->proxy('clearKeywords');
388
    }
389
390
    public function getKeywords()
391
    {
392
        return $this->proxy('getKeywords');
393
    }
394
395
    public function getTemplate()
396
    {
397
        return $this->proxy('getTemplate');
398
    }
399
400
    public function setTemplate(TemplateInterface $template)
401
    {
402
        return $this->proxy('setTemplate', $template);
403
    }
404
405
406
    /**#@-*/
407
}
408