Completed
Push — master ( bd75c7...426d7c )
by vistart
07:28
created

BaseMongoEntityModel::getRefGUID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\models;
14
15
use MongoDB\BSON\Binary;
16
use MongoDB\BSON\ObjectID;
17
use rhosocial\base\helpers\Number;
18
use rhosocial\base\helpers\IP;
19
use rhosocial\base\models\queries\BaseMongoEntityQuery;
20
use rhosocial\base\models\traits\EntityTrait;
21
use yii\mongodb\ActiveRecord;
22
23
/**
24
 * Description of BaseMongoEntityModel
25
 *
26
 * @property string $GUID GUID value in readable format (same as $readableGUID).
27
 * @property ObjectID $ID
28
 * @version 1.0
29
 * @author vistart <[email protected]>
30
 */
31
abstract class BaseMongoEntityModel extends ActiveRecord
32
{
33
    use EntityTrait;
34
    
35 27
    public function getGUIDRules()
36
    {
37 27
        $rules = [];
38 27
        if (is_string($this->guidAttribute) || !empty($this->guidAttribute)) {
39
            $rules = [
40 15
                [[$this->guidAttribute], 'required',],
41
            ];
42
        }
43 27
        return $rules;
44
    }
45
    
46
    /**
47
     * Get GUID only for referencing, avoiding conflict with 'guid' field.
48
     * @return 
49
     */
50
    public function getRefGUID()
51
    {
52
        return $this->getGUID();
53
    }
54
    
55 6
    public function getGUID()
56
    {
57 6
        $guidAttribute = $this->guidAttribute;
58 6
        return (!is_string($guidAttribute) || empty($guidAttribute)) ? null : $this->$guidAttribute->getData();
59
    }
60
    
61 27
    public function setGUID($guid)
62
    {
63 27
        $guidAttribute = $this->guidAttribute;
64 27
        if (!is_string($guidAttribute) || empty($guidAttribute)) {
65 12
            return null;
66
        }
67 15
        if (preg_match(Number::GUID_REGEX, $guid)) {
68 3
            $guid = hex2bin(str_replace(['{', '}', '-'], '', $guid));
69
        }
70 15
        return $this->$guidAttribute = new Binary($guid, Binary::TYPE_UUID);
71
    }
72
    
73
    /**
74
     * Check if the $guid existed in current database table.
75
     * @param string|Binary $guid the GUID to be checked.
76
     * @return boolean Whether the $guid exists or not.
77
     */
78 3
    public static function checkGuidExists($guid)
79
    {
80 3
        if (is_string($guid)) {
81 3
            if (strlen($guid) == 16) {
82 3
                $binary = new Binary($guid, Binary::TYPE_UUID);
83 3
            } elseif (preg_match(Number::GUID_REGEX, $guid)) {
84 3
                $binary = new Binary(Number::guid_bin($guid), Binary::TYPE_UUID);
85
            } else {
86 3
                return false;
87
            }
88 3
            return static::findOne($binary) !== null;
89
        }
90 3
        if ($guid instanceof Binary) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\Binary does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
91 3
            return static::findOne($guid) !== null;
92
        }
93 3
        return false;
94
    }
95
    
96
    /**
97
     * Get the rules associated with ip attributes.
98
     * @return array
99
     */
100 27
    public function getIPRules()
101
    {
102 27
        $rules = [];
103 27
        if ($this->enableIP & static::$ipv4) {
104
            $rules = [
105 27
                [[$this->ipAttribute],
106 27
                    'string'
107
                ],
108
            ];
109
        }
110 27
        if ($this->enableIP & static::$ipv6) {
111
            $rules = [
112 27
                [[$this->ipAttribute],
113 27
                    'string'
114
                ],
115
            ];
116
        }
117 27
        if ($this->enableIP & static::$ipAll) {
118 27
            $rules[] = [
119 27
                [$this->ipTypeAttribute], 'in', 'range' => [IP::IPv4, IP::IPv6],
120
            ];
121
        }
122 27
        return $rules;
123
    }
124
    
125
    /**
126
     * Get the IPv4 address.
127
     * @return string
128
     */
129 3
    protected function getIPv4Address()
130
    {
131 3
        return ($this->{$this->ipAttribute});
132
    }
133
    
134
    /**
135
     * Get the IPv6 address.
136
     * @return string
137
     */
138 3
    protected function getIPv6Address()
139
    {
140 3
        return ($this->{$this->ipAttribute});
141
    }
142
    
143 3
    protected function setIPv4Address($ipAddress)
144
    {
145 3
        return $this->{$this->ipAttribute} = ($ipAddress);
146
    }
147
    
148 3
    protected function setIPv6Address($ipAddress)
149
    {
150 3
        return $this->{$this->ipAttribute} = ($ipAddress);
151
    }
152
153
    /**
154
     * Initialize new entity.
155
     */
156 27
    public function init()
157
    {
158 27
        $this->idAttribute = '_id';
159 27
        $this->idAttributeType = static::$idTypeAutoIncrement;
160 27
        if ($this->skipInit) {
161 15
            return;
162
        }
163 27
        $this->initEntityEvents();
164 27
        parent::init();
165 27
    }
166
167
    /**
168
     * @inheritdoc
169
     * @return BaseMongoEntityQuery the newly created [[BaseMongoEntityQuery]] or its sub-class instance.
170
     */
171 15
    public static function find()
172
    {
173 15
        $self = static::buildNoInitModel();
174
        /* @var $self static */
175 15
        if (!is_string($self->queryClass)) {
176 3
            $self->queryClass = BaseMongoEntityQuery::class;
177
        }
178 15
        $queryClass = $self->queryClass;
179 15
        return new $queryClass(get_called_class(), ['noInitModel' => $self]);
180
    }
181
182
    /**
183
     * @inheritdoc
184
     * You can override this method if enabled fields cannot meet you requirements.
185
     * @return array
186
     */
187 15
    public function attributes()
188
    {
189 15
        return $this->enabledFields();
190
    }
191
}
192