Completed
Push — master ( 621c80...c2185a )
by Vitaly
02:34
created

Manager::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9714
cc 3
eloc 11
nc 3
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Manager::instance() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 30.11.15
6
 * Time: 16:38
7
 */
8
namespace samsonframework\orm;
9
10
/**
11
 * Database entity manager.
12
 * @package samsonframework\orm
13
 */
14
class Manager
15
{
16
    /** @var string Entity identifier */
17
    protected $entityName;
18
19
    /** @var string Entity primary field name */
20
    protected $primaryFieldName;
21
22
    /** @var array Collection of entity field names and their types */
23
    protected $attributes = array();
24
25
    /**
26
     * Manager constructor.
27
     *
28
     * @param string $entityName Entity name
29
     * @param array $attributes Key-value collection with field name => type
30
     * @param \PDO $driver database low-level driver
31
     */
32
    public function __construct($entityName, $attributes, $driver)
33
    {
34
        $this->entityName = $entityName;
35
        $this->attributes = $attributes;
36
        $this->driver = $driver;
0 ignored issues
show
Bug introduced by
The property driver does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
    }
38
39
    /**
40
     * Get new entity instance.
41
     *
42
     * @return RecordInterface New database manager entity instance
43
     */
44
    public function instance()
45
    {
46
        return new $this->entityName($this);
47
    }
48
49
    /**
50
     * Convert RecordInterface instance to collection of its field name => value.
51
     *
52
     * @param RecordInterface $object Database record instance to convert
53
     * @return array Collection of key => value with SQL fields statements
54
     */
55
    protected function &getFields(RecordInterface &$object = null)
56
    {
57
        $collection = array();
58
        foreach ($this->attributes as $attribute => $type) {
59
            if ($type == 'timestamp') {
60
                continue;
61
            } elseif ($this->primaryFieldName == $attribute) {
62
                continue;
63
            }
64
65
            $collection[$attribute] = $this->quote($object->$attribute);
0 ignored issues
show
Bug introduced by
The method quote() does not seem to exist on object<samsonframework\orm\Manager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67
68
        return $collection;
69
    }
70
71
    /**
72
     * Create new database entity record.
73
     * @param RecordInterface $entity Entity record for creation
74
     * @return RecordInterface Created database entity record with new primary identifier
75
     */
76
    public function create(RecordInterface $entity)
77
    {
78
        $fields = $this->getFields($entity);
79
80
        $this->execute('INSERT INTO `' . $this->entityName . '` (`'
0 ignored issues
show
Bug introduced by
The method execute() does not seem to exist on object<samsonframework\orm\Manager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            . implode('`,`', array_keys($fields)) . '`) VALUES (' . implode(',', $fields) . ')'
82
        );
83
    }
84
85
    /**
86
     * Read database entity records from QueryInterface.
87
     *
88
     * @param QueryInterface $query For retrieving records
89
     * @return RecordInterface[] Collection of read database entity records
90
     */
91
    public function read(QueryInterface $query)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        // TODO: Implement read() method.
94
    }
95
96
    /**
97
     * Update database entity record.
98
     *
99
     * @param RecordInterface $entity Entity record for updating
100
     */
101
    public function update(RecordInterface $entity)
102
    {
103
        // Generate entity fields update command
104
        $fields = array();
105
        foreach ($this->getFields($entity) as $fieldName => $fieldValue) {
106
            $fields[] = '`'.$this->entityName.'`.`'.$fieldName.'` = "'.$fieldValue.'"';
107
        }
108
109
        $this->execute('UPDATE `' . $this->entityName . '` SET '
0 ignored issues
show
Bug introduced by
The method execute() does not seem to exist on object<samsonframework\orm\Manager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
            . implode(',', $fields)
111
            . ' WHERE `' . $this->entityName . '`.`' . $this->primaryFieldName . '`="'
112
            . $this->quote($entity->id) . '"');
0 ignored issues
show
Bug introduced by
Accessing id on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
The method quote() does not seem to exist on object<samsonframework\orm\Manager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
    }
114
115
    /**
116
     * Delete database record from database.
117
     *
118
     * @param RecordInterface $entity Entity record for removing
119
     */
120
    public function delete(RecordInterface $entity)
121
    {
122
        $this->execute('DELETE FROM `' . $this->entityName . '` WHERE '
0 ignored issues
show
Bug introduced by
The method execute() does not seem to exist on object<samsonframework\orm\Manager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
            . $this->primaryFieldName . ' = "' . $this->quote($entity->id) . '"'
0 ignored issues
show
Bug introduced by
Accessing id on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
The method quote() does not seem to exist on object<samsonframework\orm\Manager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
        );
125
    }
126
}
127