Model::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Simply\Database;
4
5
/**
6
 * A class the defines that possible interactions with a database record.
7
 * @author Riikka Kalliomäki <[email protected]>
8
 * @copyright Copyright (c) 2018 Riikka Kalliomäki
9
 * @license http://opensource.org/licenses/mit-license.php MIT License
10
 */
11
class Model
12
{
13
    /** @var Record The database record for the model */
14
    protected $record;
15
16
    /**
17
     * Model constructor.
18
     * @param Record $record The database record for the model
19
     */
20 15
    protected function __construct(Record $record)
21
    {
22 15
        $this->record = $record;
23 15
    }
24
25
    /**
26
     * Initializes the model with the given database record instead of calling the default constructor.
27
     * @param Record $record The database record for the model
28
     * @return Model A new initialized model with the given database record
29
     */
30 20
    public static function createFromDatabaseRecord(Record $record): self
31
    {
32
        /** @var Model $model */
33 20
        $model = (new \ReflectionClass(static::class))->newInstanceWithoutConstructor();
34 20
        $model->record = $record;
35
36 20
        return $model;
37
    }
38
39
    /**
40
     * Returns the database record for the model.
41
     * @return Record The database record for the model
42
     */
43 18
    public function getDatabaseRecord(): Record
44
    {
45 18
        return $this->record;
46
    }
47
}
48