Completed
Push — master ( a93b08...8d3934 )
by Stephan
02:44 queued 46s
created

Findable::findId()   B

Complexity

Conditions 7
Paths 25

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 8.2222
cc 7
eloc 14
nc 25
nop 2
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
    public function findWithSelect($id, $select = '')
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
    public function getResultSet(array $params = [])
122
    {
123
        return new Resultset($this->connection(), $this->url, get_class($this), $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...
124
    }
125
126
    public function get(array $params = [])
127
    {
128
        $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...
129
130
        return $this->collectionFromResult($result);
131
    }
132
133
134
    public function collectionFromResult($result)
135
    {
136
        // If we have one result which is not an assoc array, make it the first element of an array for the
137
        // collectionFromResult function so we always return a collection from filter
138
        if ((bool) count(array_filter(array_keys($result), 'is_string'))) {
139
            $result = [ $result ];
140
        }
141
142
        while ($this->connection()->nextUrl !== null)
143
        {
144
            $nextResult = $this->connection()->get($this->connection()->nextUrl);
145
            
146
            // If we have one result which is not an assoc array, make it the first element of an array for the array_merge function
147
            if ((bool) count(array_filter(array_keys($nextResult), 'is_string'))) {
148
                $nextResult = [ $nextResult ];
149
            }
150
            
151
            $result = array_merge($result, $nextResult);
152
        }
153
        $collection = [ ];
154
        foreach ($result as $r) {
155
            $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

155
            $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...
156
        }
157
158
        return $collection;
159
    }
160
}
161