Passed
Push — master ( a3cc07...eb52df )
by Nashwan
03:50
created

City::doActionOnPrePersist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * City entity class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Models
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Models;
10
11
use DateTime;
12
use DateTimeZone;
13
use EBloodBank as EBB;
14
use InvalidArgumentException;
15
16
/**
17
 * City entity class
18
 *
19
 * @since 1.0
20
 *
21
 * @Entity(repositoryClass="EBloodBank\Models\CityRepository")
22
 * @Table(name="city")
23
 * @HasLifecycleCallbacks
24
 */
25
class City extends Entity
26
{
27
    /**
28
     * City ID
29
     * 
30
     * @var   int
31
     * @since 1.0
32
     *
33
     * @Id
34
     * @GeneratedValue
35
     * @Column(type="integer", name="city_id")
36
     */
37
    protected $id = 0;
38
39
    /**
40
     * City name
41
     * 
42
     * @var   string
43
     * @since 1.0
44
     *
45
     * @Column(type="string", name="city_name", unique=true)
46
     */
47
    protected $name;
48
49
    /**
50
     * City creation datetime
51
     * 
52
     * @var   \DateTime
53
     * @since 1.0
54
     *
55
     * @Column(type="datetime", name="city_created_at")
56
     */
57
    protected $created_at;
58
59
    /**
60
     * City created by
61
     * 
62
     * @var   User
63
     * @since 1.0
64
     *
65
     * @ManyToOne(targetEntity="EBloodBank\Models\User")
66
     * @JoinColumn(name="city_created_by", referencedColumnName="user_id")
67
     */
68
    protected $created_by;
69
70
    /**
71
     * City districts
72
     * 
73
     * @var   District[]
74
     * @since 1.0
75
     *
76
     * @OneToMany(targetEntity="EBloodBank\Models\District", mappedBy="city")
77
     */
78
    protected $districts = [];
79
80
    /**
81
     * @return bool
82
     * @since  1.0
83
     */
84
    public function isExists()
85
    {
86
        $id = (int) $this->get('id');
87
        return ! empty($id);
88
    }
89
90
    /**
91
     * @return void
92
     * @since  1.6
93
     * 
94
     * @PrePersist
95
     */
96
    public function doActionOnPrePersist()
97
    {
98
        $this->set('created_at', new DateTime('now', new DateTimeZone('UTC')));
99
    }
100
101
    /**
102
     * @return mixed
103
     * @since  1.0
104
     * @static
105
     */
106
    public static function sanitize($key, $value)
107
    {
108
        switch ($key) {
109
            case 'id':
110
                $value = EBB\sanitizeInteger($value);
0 ignored issues
show
Bug introduced by
The function sanitizeInteger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeInteger($value);
Loading history...
111
                break;
112
            case 'name':
113
                $value = EBB\sanitizeTitle($value);
0 ignored issues
show
Bug introduced by
The function sanitizeTitle was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeTitle($value);
Loading history...
114
                break;
115
        }
116
117
        return $value;
118
    }
119
120
    /**
121
     * @throws \InvalidArgumentException
122
     * @return bool
123
     * @since  1.0
124
     * @static
125
     */
126
    public static function validate($key, $value)
127
    {
128
        switch ($key) {
129
            case 'id':
130
                if (! EBB\isValidID($value)) {
0 ignored issues
show
Bug introduced by
The function isValidID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
                if (! /** @scrutinizer ignore-call */ EBB\isValidID($value)) {
Loading history...
131
                    throw new InvalidArgumentException(__('Invalid city ID.'));
132
                }
133
                break;
134
            case 'name':
135
                if (! is_string($value) || empty($value)) {
136
                    throw new InvalidArgumentException(__('Invalid city name.'));
137
                }
138
                break;
139
            case 'created_by':
140
                if (! $value instanceof User || ! $value->isExists()) {
141
                    throw new InvalidArgumentException(__('Invalid city originator.'));
142
                }
143
                break;
144
        }
145
146
        return true;
147
    }
148
}
149