Completed
Push — master ( 3539dd...b3ee9f )
by Stephan
02:55 queued 01:12
created

Findable::find()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 8
nc 4
nop 1
1
<?php namespace Picqer\Financials\Exact\Query;
2
3
use Picqer\Financials\Exact\Connection;
4
5
trait Findable
6
{
7
    /**
8
     * @return Connection
9
     */
10
    abstract function connection();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
11
12
    abstract function isFillable($key);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
14
    public function find($id)
15
    {
16
    	$filter = $this->primaryKey . " eq guid'$id'";
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on Picqer\Financials\Exact\Query\Findable. Did you maybe forget to declare it?
Loading history...
17
18
        if ($this->primaryKey === 'Code') {
19
            $filter = $this->primaryKey . " eq $id";
20
        }
21
22
        $records = $this->connection()->get($this->url, [
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on Picqer\Financials\Exact\Query\Findable. Did you maybe forget to declare it?
Loading history...
23
            '$filter' => $filter,
24
            '$top' => 1, // The result will always be 1 but on some entities Exact gives an error without it.
25
        ]);
26
27
        $result = isset($records[0]) ? $records[0] : [];
28
        return new self($this->connection(), $result);
0 ignored issues
show
Unused Code introduced by
The call to Picqer\Financials\Exact\...Findable::__construct() has too many arguments starting with $this->connection(). ( Ignorable by Annotation )

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

28
        return /** @scrutinizer ignore-call */ new self($this->connection(), $result);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
29
    }
30
31 View Code Duplication
    public function findWithSelect($id, $select = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        //eg: $oAccounts->findWithSelect('5b7f4515-b7a0-4839-ac69-574968677d96', 'Code, Name');
34
        $result = $this->connection()->get($this->url, [
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on Picqer\Financials\Exact\Query\Findable. Did you maybe forget to declare it?
Loading history...
35
            '$filter' => $this->primaryKey . " eq guid'$id'",
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on Picqer\Financials\Exact\Query\Findable. Did you maybe forget to declare it?
Loading history...
36
            '$select' => $select
37
        ]);
38
39
        return new self($this->connection(), $result);
0 ignored issues
show
Unused Code introduced by
The call to Picqer\Financials\Exact\...Findable::__construct() has too many arguments starting with $this->connection(). ( Ignorable by Annotation )

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

39
        return /** @scrutinizer ignore-call */ new self($this->connection(), $result);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
40
    }
41
42
43
    /**
44
     * Return the value of the primary key
45
     *
46
     * @param string $code the value to search for
47
     * @param string $key  the key being searched (defaults to 'Code')
48
     *
49
     * @return string (guid)
50
     */
51
    public function findId($code, $key='Code'){
52
        if ( $this->isFillable($key) ) {
53
            $format = ($this->url == 'crm/Accounts' && $key === 'Code') ? '%18s' : '%s';
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on Picqer\Financials\Exact\Query\Findable. Did you maybe forget to declare it?
Loading history...
54
            if (preg_match('/^[\w]{8}-([\w]{4}-){3}[\w]{12}$/', $code)) {
55
                $format = "guid'$format'";
56
            }
57
            elseif (is_string($code)) {
58
                $format = "'$format'";
59
            }
60
61
            $filter = sprintf("$key eq $format", $code);
62
            $request = [
63
                '$filter' => $filter,
64
                '$top' => 1,
65
                '$select' => $this->primaryKey,
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on Picqer\Financials\Exact\Query\Findable. Did you maybe forget to declare it?
Loading history...
66
                '$orderby' => $this->primaryKey,
67
            ];
68
            if( $records = $this->connection()->get($this->url, $request) ){
69
                return $records[0][$this->primaryKey];
70
            }
71
        }
72
    }
73
74
75
    public function filter($filter, $expand = '', $select = '', $system_query_options = null, array $headers = [])
76
    {
77
        $originalDivision = $this->connection()->getDivision();
78
79
        if ($this->isFillable('Division') && preg_match("@Division[\t\r\n ]+eq[\t\r\n ]+([0-9]+)@i", $filter, $divisionId)) {
80
            $this->connection()->setDivision($divisionId[1]); // Fix division
81
        }
82
83
        $request = [
84
            '$filter' => $filter
85
        ];
86
        if (strlen($expand) > 0) {
87
            $request['$expand'] = $expand;
88
        }
89
        if (strlen($select) > 0) {
90
            $request['$select'] = $select;
91
        }
92
        if (is_array($system_query_options)) {
93
            // merge in other options
94
            // no verification of proper system query options
95
            $request = array_merge($system_query_options, $request);
96
        }
97
98
        $result = $this->connection()->get($this->url, $request, $headers);
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on Picqer\Financials\Exact\Query\Findable. Did you maybe forget to declare it?
Loading history...
99
100
        if (!empty($divisionId)) {
101
            $this->connection()->setDivision($originalDivision); // Restore division
102
        }
103
104
        return $this->collectionFromResult($result);
105
    }
106
107
108
    /**
109
     * Returns the first Financial model in by applying $top=1 to the query string.
110
     *
111
     * @return \Picqer\Financials\Exact\Model|null
112
     */
113
    public function first()
114
    {
115
        $results = $this->filter('', '', '', ['$top'=> 1]);
116
        $result = is_array($results) && count($results) > 0 ? $results[0] : null;
117
118
        return $result;
119
    }
120
121
122
    public function get(array $params = [])
123
    {
124
        $result = $this->connection()->get($this->url, $params);
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on Picqer\Financials\Exact\Query\Findable. Did you maybe forget to declare it?
Loading history...
125
126
        return $this->collectionFromResult($result);
127
    }
128
129
130
    public function collectionFromResult($result)
131
    {
132
        // If we have one result which is not an assoc array, make it the first element of an array for the
133
        // collectionFromResult function so we always return a collection from filter
134
        if ((bool) count(array_filter(array_keys($result), 'is_string'))) {
135
            $result = [ $result ];
136
        }
137
138
        while ($this->connection()->nextUrl !== null)
139
        {
140
            $nextResult = $this->connection()->get($this->connection()->nextUrl);
141
            
142
            // If we have one result which is not an assoc array, make it the first element of an array for the array_merge function
143
            if ((bool) count(array_filter(array_keys($nextResult), 'is_string'))) {
144
                $nextResult = [ $nextResult ];
145
            }
146
            
147
            $result = array_merge($result, $nextResult);
148
        }
149
        $collection = [ ];
150
        foreach ($result as $r) {
151
            $collection[] = new self($this->connection(), $r);
0 ignored issues
show
Unused Code introduced by
The call to Picqer\Financials\Exact\...Findable::__construct() has too many arguments starting with $this->connection(). ( Ignorable by Annotation )

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

151
            $collection[] = /** @scrutinizer ignore-call */ new self($this->connection(), $r);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
152
        }
153
154
        return $collection;
155
    }
156
}
157