Passed
Branchfeature/useWidgetsNamespaces (54f503)
by Robin
04:17
created

AppCustomContainer::isVisibleForRecord()   C

Complexity

Conditions 12
Paths 51

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 34
c 1
b 0
f 1
dl 0
loc 58
rs 6.9666
cc 12
nc 51
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// -------------------------------------------------------------------------
4
// OVIDENTIA http://www.ovidentia.org
5
// Ovidentia is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2, or (at your option)
8
// any later version.
9
//
10
// This program is distributed in the hope that it will be useful, but
11
// WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
// See the GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18
// USA.
19
// -------------------------------------------------------------------------
20
/**
21
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
22
 * @copyright Copyright (c) 2022 by SI4YOU ({@link https://www.siforyou.com})
23
 */
24
namespace Capwelton\LibApp\Set;
25
26
use bab_charset;
27
use Capwelton\LibOrm\MySql\ORMMySqlIterator;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\MySql\ORMMySqlIterator 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...
28
29
/**
30
 * @property string $name
31
 * @property string $description
32
 * @property string $object
33
 * @property string $view
34
 * @property string $fields
35
 * @property string $classname
36
 * @property string $sizePolicy
37
 * @property string $layout
38
 * @property int $rank
39
 * @property string $visibilityCriteria
40
 *
41
 * @method Func_App App()
42
 */
43
class AppCustomContainer extends AppTraceableRecord
44
{
45
    
46
    const LAYOUT_VERTICAL_BOX = 'vbox';
47
    
48
    const LAYOUT_HORIZONTAL_BOX = 'hbox';
49
    
50
    const LAYOUT_FLOW = 'flow';
51
    
52
    /**
53
     * @return string[]
54
     */
55
    public static function getLayouts()
56
    {
57
        static $layouts = null;
58
        
59
        if(! isset($layouts)){
60
            $layouts = array(
61
                self::LAYOUT_VERTICAL_BOX => app_translate('Vertical box'),
62
                self::LAYOUT_HORIZONTAL_BOX => app_translate('Horizontal box'),
63
                self::LAYOUT_FLOW => app_translate('Flow box')
64
            );
65
        }
66
        return $layouts;
67
    }
68
    
69
    /**
70
     * @param AppRecord $record
71
     *
72
     * @return bool
73
     */
74
    public function isVisibleForRecord(AppRecord $record)
75
    {
76
        if(empty($this->visibilityCriteria)){
77
            return true;
78
        }
79
        
80
        $App = \bab_functionality::get('App');
81
        $recordSet = $record->getParentSet();
82
        
83
        $arrCriteria = json_decode($this->visibilityCriteria, true);
84
        if(bab_charset::getIso() != bab_charset::UTF_8){
85
            $arrCriteria = app_utf8Encode($arrCriteria);
86
        }
87
        foreach ($arrCriteria as $fieldName => $condition){
88
            if(strpos($fieldName, '/') !== false){
89
                list ($oneField, $foreignField) = explode('/', $fieldName);
90
                $field = $recordSet->$oneField()->$foreignField;
91
            }
92
            else{
93
                $field = $recordSet->$fieldName;
94
            }
95
            foreach ($condition as $op => $value){
96
                if(! is_array($value)){
97
                    $criteria = $field->$op($value);
98
                }
99
                else{
100
                    foreach ($value as $foreignClassName => $foreignValues){
101
                        $foreignClassName = str_replace($App->classPrefix, '', $foreignClassName) . 'Set';
102
                        $foreignSet = $App->$foreignClassName();
103
                        $foreignField = '';
104
                        $foreignCondition = '';
105
                        foreach ($foreignValues as $foreignFieldName => $foreignConditions){
106
                            if($foreignFieldName === '_foreignField'){
107
                                $foreignField = $foreignConditions;
108
                            }
109
                            else{
110
                                foreach ($foreignConditions as $key => $val){
111
                                    $foreignCondition = $foreignSet->$foreignFieldName->$key($val);
112
                                }
113
                            }
114
                        }
115
                    }
116
                    if(method_exists($foreignSet, 'getDefaultCriteria')){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $foreignSet does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
                        $foreignCondition = $foreignCondition->_AND_($foreignSet->getDefaultCriteria());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $foreignCondition does not seem to be defined for all execution paths leading up to this point.
Loading history...
118
                    }
119
                    $criteria = $field->in($foreignCondition, $foreignField);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $foreignField does not seem to be defined for all execution paths leading up to this point.
Loading history...
120
                }
121
            }
122
        }
123
        
124
        $criteria = $criteria->_AND_($recordSet->id->is($record->id));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $criteria does not seem to be defined for all execution paths leading up to this point.
Loading history...
125
        
126
        /**
127
         * @var ORMMySqlIterator
128
         */
129
        $records = $recordSet->select($criteria);
130
        
131
        return ($records->count() != 0);
132
    }
133
}
134