Setter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 55
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperties() 0 7 2
A setLoadedProperties() 0 10 2
A setLoadedAllProperties() 0 5 2
A setLoaded() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Scrawler package.
5
 *
6
 * (c) Pranjal Pandey <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Scrawler\Arca\Traits\Model;
13
14
use Doctrine\DBAL\Types\Type;
15
use Scrawler\Arca\Model;
16
17
/**
18
 * Setter trait to provide setter methods to the model.
19
 */
20
trait Setter
21
{
22
    /**
23
     * Set all properties of model via array.
24
     *
25
     * @param array<mixed> $properties
26
     */
27
    public function setProperties(array $properties): Model
28
    {
29
        foreach ($properties as $key => $value) {
30
            $this->set($key, $value);
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

30
            $this->/** @scrutinizer ignore-call */ 
31
                   set($key, $value);
Loading history...
31
        }
32
33
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Scrawler\Arca\Traits\Model\Setter which includes types incompatible with the type-hinted return Scrawler\Arca\Model.
Loading history...
34
    }
35
36
    /**
37
     * Set all properties of model loaded via database.
38
     *
39
     * @param array<mixed> $properties
40
     */
41
    public function setLoadedProperties(array $properties): Model
42
    {
43
        $this->__properties['self'] = $properties;
0 ignored issues
show
Bug Best Practice introduced by
The property __properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
        $this->setLoadedAllProperties($properties);
45
        foreach (array_keys($properties) as $key) {
46
            $this->__properties['type'][$key] = $this->tableManager->getTable($this->table)->getColumn($key)->getComment();
47
        }
48
        $this->__meta['id'] = $properties['id'];
0 ignored issues
show
Bug Best Practice introduced by
The property __meta does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
50
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Scrawler\Arca\Traits\Model\Setter which includes types incompatible with the type-hinted return Scrawler\Arca\Model.
Loading history...
51
    }
52
53
    /**
54
     * get php value from database value.
55
     *
56
     * @param array<mixed> $properties
57
     */
58
    private function setLoadedAllProperties(array $properties): void
59
    {
60
        foreach ($properties as $key => $value) {
61
            $type = Type::getType($this->tableManager->getTable($this->table)->getColumn($key)->getComment());
62
            $this->__properties['all'][$key] = $type->convertToPHPValue($value, $this->connection->getDatabasePlatform());
0 ignored issues
show
Bug Best Practice introduced by
The property __properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
        }
64
    }
65
66
    /**
67
     * call when model is loaded from database.
68
     */
69
    public function setLoaded(): Model
70
    {
71
        $this->__meta['is_loaded'] = true;
0 ignored issues
show
Bug Best Practice introduced by
The property __meta does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
        $this->__meta['id_error'] = false;
73
74
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Scrawler\Arca\Traits\Model\Setter which includes types incompatible with the type-hinted return Scrawler\Arca\Model.
Loading history...
75
    }
76
}
77