Model   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 35
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getDatabaseRecord() 0 3 1
A createFromDatabaseRecord() 0 7 1
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