Completed
Push — develop ( c5e17b...b595d4 )
by Nate
04:27
created

LookupAssociationTrait::findOrganization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\actions\organizations;
10
11
use Craft;
12
use craft\elements\User;
13
use flipbox\organizations\elements\Organization;
14
use yii\db\ActiveRecord;
15
use yii\web\HttpException;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 3.0.0
20
 *
21
 * @method ActiveRecord populate(ActiveRecord $record)
22
 */
23
trait LookupAssociationTrait
24
{
25
    /**
26
     * HTTP not found response code
27
     *
28
     * @return int
29
     */
30
    protected function statusCodeNotFound(): int
31
    {
32
        return $this->statusCodeNotFound ?? 404;
0 ignored issues
show
Bug introduced by
The property statusCodeNotFound does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    protected function messageNotFound(): string
39
    {
40
        return $this->messageNotFound ?? 'Unable to find object.';
0 ignored issues
show
Bug introduced by
The property messageNotFound does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
    }
42
43
    /**
44
     * @return null
45
     * @throws HttpException
46
     */
47
    protected function handleNotFoundResponse()
48
    {
49
        throw new HttpException(
50
            $this->statusCodeNotFound(),
51
            $this->messageNotFound()
52
        );
53
    }
54
55
    /**
56
     * @param string|int $identifier
57
     * @return ORganization|null
58
     */
59
    protected function findOrganization($identifier)
60
    {
61
        return Organization::findOne($identifier);
62
    }
63
64
    /**
65
     * @param string|int $identifier
66
     * @return User|null
67
     */
68
    protected function findUser($identifier)
69
    {
70
        if (is_numeric($identifier)) {
71
            return Craft::$app->getUsers()->getUserById($identifier);
72
        }
73
74
        return Craft::$app->getUsers()->getUserByUsernameOrEmail($identifier);
75
    }
76
}
77