AbstractCustomFieldsModel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 64
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllCustomFields() 0 42 4
A beforeCreate() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\CustomFields;
5
6
use Canvas\Models\CustomFieldsModules;
7
use Canvas\Models\AbstractModel;
8
use Baka\Database\Contracts\CustomFields\CustomFieldsTrait;
0 ignored issues
show
Bug introduced by
The type Baka\Database\Contracts\...ields\CustomFieldsTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use PDO;
10
11
/**
12
 * Custom Fields Abstract Class.
13
 * @property \Phalcon\Di $di
14
 */
15
abstract class AbstractCustomFieldsModel extends AbstractModel
16
{
17
    use CustomFieldsTrait;
18
19
    /**
20
     * Get all custom fields of the given object.
21
     *
22
     * @param  array  $fields
23
     * @return array
24
     */
25
    public function getAllCustomFields(array $fields = [])
26
    {
27
        //no di? ok so no custom fields
28
        if (!$this->di->has('userData')) {
29
            return ;
30
        }
31
32
        //We does it only find names in plural? We need to fix this or make a workaroun
33
        if (!$models = CustomFieldsModules::findFirstByName($this->getSource())) {
34
            return;
35
        }
36
37
        $bind = [$this->getId(), $this->di->getApp()->getId(), $models->getId(), $this->di->getUserData()->currentCompanyId()];
38
39
        // $customFieldsValueTable = $this->getSource() . '_custom_fields';
40
        $customFieldsValueTable = $this->getSource() . '_custom_fields';
41
42
        //We are to make a new query to replace old Canvas implementation.
43
        $result = $this->getReadConnection()->prepare("SELECT l.{$this->getSource()}_id,
44
                                               c.id as field_id,
45
                                               c.name,
46
                                               l.value ,
47
                                               c.users_id,
48
                                               l.created_at,
49
                                               l.updated_at
50
                                        FROM {$customFieldsValueTable} l,
51
                                             custom_fields c
52
                                        WHERE c.id = l.custom_fields_id
53
                                          AND l.{$this->getSource()}_id = ?
54
                                          AND c.apps_id = ?
55
                                          AND c.custom_fields_modules_id = ?
56
                                          AND c.companies_id = ? ");
57
58
        $result->execute($bind);
59
60
        $listOfCustomFields = [];
61
62
        while ($row = $result->fetch(PDO::FETCH_OBJ)) {
63
            $listOfCustomFields[$row->name] = $row->value;
64
        }
65
66
        return $listOfCustomFields;
67
    }
68
69
    /**
70
    * Before create.
71
    *
72
    * @return void
73
    */
74
    public function beforeCreate()
75
    {
76
        $this->created_at = date('Y-m-d H:i:s');
0 ignored issues
show
Bug Best Practice introduced by
The property created_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
        $this->updated_at = null;
0 ignored issues
show
Bug Best Practice introduced by
The property updated_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
        $this->is_deleted = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property is_deleted does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
    }
80
}
81