Test Failed
Pull Request — master (#20)
by
unknown
03:25
created

AbstractCustomFieldsModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 56%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 74
ccs 14
cts 25
cp 0.56
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCustomFieldsByModel() 0 11 4
A getAllCustomFields() 0 47 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\CustomFields;
5
6
use Gewaer\Models\Modules;
7
8
/**
9
 * Custom Fields Abstract Class
10
 * @property \Phalcon\Di $di
11
 */
12
abstract class AbstractCustomFieldsModel extends \Baka\Database\ModelCustomFields
13
{
14
    /**
15
     * Get all custom fields of the given object
16
     *
17
     * @param  array  $fields
18
     * @return \Phalcon\Mvc\Model
19
     */
20 4
    public function getAllCustomFields(array $fields = [])
21
    {
22
        //We does it only find names in plural? We need to fix this or make a workaroun
23 4
        if (!$models = Modules::findFirstByName($this->getSource())) {
0 ignored issues
show
Bug introduced by
The method findFirstByName() does not exist on Gewaer\Models\Modules. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

23
        if (!$models = Modules::/** @scrutinizer ignore-call */ findFirstByName($this->getSource())) {
Loading history...
24
            return;
25
        }
26
27 4
        $fieldsIn = null;
28
29 4
        if (!empty($fields)) {
30
            $fieldsIn = " and name in ('" . implode("','", $fields) . ')';
31
        }
32
33 4
        $conditions = 'modules_id = ? ' . $fieldsIn;
0 ignored issues
show
Unused Code introduced by
The assignment to $conditions is dead and can be removed.
Loading history...
34
35 4
        $bind = [$this->getId(), $this->di->getApp()->getId(), $models->getId(), $this->di->getUserData()->default_company];
36
37
        // $customFieldsValueTable = $this->getSource() . '_custom_fields';
38 4
        $customFieldsValueTable = $this->getSource() . '_custom_fields';
39
40
        //We are to make a new query to replace old gewaer implementation.
41 4
        $result = $this->getReadConnection()->prepare("SELECT l.{$this->getSource()}_id,
1 ignored issue
show
Bug introduced by
The method prepare() does not exist on Phalcon\Db\AdapterInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Phalcon\Db\Adapter. Are you sure you never get one of those? ( Ignorable by Annotation )

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

41
        $result = $this->getReadConnection()->/** @scrutinizer ignore-call */ prepare("SELECT l.{$this->getSource()}_id,
Loading history...
42
                                               c.id as field_id,
43
                                               c.name,
44
                                               l.value ,
45
                                               c.users_id,
46
                                               l.created_at,
47
                                               l.updated_at
48 4
                                        FROM {$customFieldsValueTable} l,
49
                                             custom_fields c
50
                                        WHERE c.id = l.custom_fields_id
51 4
                                          AND l.{$this->getSource()}_id = ?
52
                                          AND c.apps_id = ?
53
                                          AND c.modules_id = ?
54
                                          AND c.companies_id = ? 
55
                                          AND l.companies_id = c.companies_id");
56
57 4
        $result->execute($bind);
58
59
        // $listOfCustomFields = $result->fetchAll();
60 4
        $listOfCustomFields = [];
61
62 4
        while ($row = $result->fetch(\PDO::FETCH_OBJ)) {
63
            $listOfCustomFields[$row->name] = $row->value;
64
        }
65
66 4
        return $listOfCustomFields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $listOfCustomFields returns the type array which is incompatible with the documented return type Phalcon\Mvc\Model.
Loading history...
67
    }
68
69
    /**
70
     * Get all custom fields of the given model
71
     *
72
     * @param  array  $fields
73
     * @return \Phalcon\Mvc\Model
74
     */
75
    public function getCustomFieldsByModel($modelName)
76
    {
77
        if (!$module = Modules::findFirstByName($modelName)) {
78
            return;
79
        }
80
        $allFields = [];
81
        if ($fields = \Gewaer\CustomFields\CustomFields::findByModulesId($module->id)->toArray()) {
0 ignored issues
show
Bug introduced by
The method findByModulesId() does not exist on Gewaer\CustomFields\CustomFields. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

81
        if ($fields = \Gewaer\CustomFields\CustomFields::/** @scrutinizer ignore-call */ findByModulesId($module->id)->toArray()) {
Loading history...
82
            foreach ($fields as $field) {
83
                array_push($allFields, $field['name']);
84
            }
85
            return $allFields;
86
        }
87
    }
88
}
89