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

getCustomFieldsByModel()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\CustomFields;
5
6
use Gewaer\Models\CustomFieldsModules;
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 array
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 = CustomFieldsModules::findFirstByName($this->getSource())) {
1 ignored issue
show
Bug introduced by
The method findFirstByName() does not exist on Gewaer\Models\CustomFieldsModules. 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 = CustomFieldsModules::/** @scrutinizer ignore-call */ findFirstByName($this->getSource())) {
Loading history...
24
            return;
25
        }
26
27 4
        $fieldsIn = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $fieldsIn is dead and can be removed.
Loading history...
28
29 4
        if (!empty($fields)) {
30
            $fieldsIn = " and name in ('" . implode("','", $fields) . ')';
31
        }
32
33 4
        $bind = [$this->getId(), $this->di->getApp()->getId(), $models->getId(), $this->di->getUserData()->default_company];
34
35
        // $customFieldsValueTable = $this->getSource() . '_custom_fields';
36 4
        $customFieldsValueTable = $this->getSource() . '_custom_fields';
37
38
        //We are to make a new query to replace old gewaer implementation.
39 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

39
        $result = $this->getReadConnection()->/** @scrutinizer ignore-call */ prepare("SELECT l.{$this->getSource()}_id,
Loading history...
40
                                               c.id as field_id,
41
                                               c.name,
42
                                               l.value ,
43
                                               c.users_id,
44
                                               l.created_at,
45
                                               l.updated_at
46 4
                                        FROM {$customFieldsValueTable} l,
47
                                             custom_fields c
48
                                        WHERE c.id = l.custom_fields_id
49 4
                                          AND l.{$this->getSource()}_id = ?
50
                                          AND c.apps_id = ?
51
                                          AND c.custom_fields_modules_id = ?
52
                                          AND c.companies_id = ? 
53
                                          AND l.companies_id = c.companies_id");
54
55 4
        $result->execute($bind);
56
57
        // $listOfCustomFields = $result->fetchAll();
58 4
        $listOfCustomFields = [];
59
60 4
        while ($row = $result->fetch(\PDO::FETCH_OBJ)) {
61
            $listOfCustomFields[$row->name] = $row->value;
62
        }
63
64 4
        return $listOfCustomFields;
65
    }
66
67
    /**
68
     * Get all custom fields of the given model
69
     *
70
     * @param  array  $fields
71
     * @return \Phalcon\Mvc\Model
72
     */
73
    public function getCustomFieldsByModel($modelName)
74
    {
75
        if (!$module = CustomFieldsModules::findFirstByName($modelName)) {
76
            return;
77
        }
78
        $allFields = [];
79
        if ($fields = CustomFields::findByModulesId($module->id)->toArray()) {
1 ignored issue
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

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