Passed
Branch master (61755c)
by Robert
02:34
created

EngagementHistory::next()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 11
nop 0
1
<?php
2
3
namespace LivePersonInc\LiveEngageLaravel\Collections;
4
5
use Illuminate\Support\Collection;
6
use LivePersonInc\LiveEngageLaravel\LiveEngageLaravel;
7
use LivePersonInc\LiveEngageLaravel\Models\Engagement;
8
use LivePersonInc\LiveEngageLaravel\Models\Info;
9
use LivePersonInc\LiveEngageLaravel\Models\Visitor;
10
use LivePersonInc\LiveEngageLaravel\Models\Campaign;
11
12
class EngagementHistory extends Collection
13
{
14
    private $instance;
15
16
    public function __construct(array $models = [], LiveEngageLaravel $instance = null)
17
    {
18
        $this->instance = $instance;
19
20
        parent::__construct($models);
21
    }
22
    
23
    public function find($engagementID)
24
    {
25
	    $result = $this->filter(function ($value, $key) use ($engagementID) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

25
	    $result = $this->filter(function ($value, /** @scrutinizer ignore-unused */ $key) use ($engagementID) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
		    return $value->info->sessionId == $engagementID;
27
	    });
28
	    
29
	    return $result->first();
30
    }
31
32
    public function next()
33
    {
34
        if (! $this->instance) {
35
            return false;
36
        }
37
38
        $instance = $this->instance;
39
40
        $next = $instance->retrieveHistory($instance->start, $instance->end, $instance->next);
0 ignored issues
show
Bug Best Practice introduced by
The property $end is declared private in LivePersonInc\LiveEngageLaravel\LiveEngageLaravel. Since you implement __get, consider adding a @property or @property-read.
Loading history...
Bug Best Practice introduced by
The property $start is declared private in LivePersonInc\LiveEngageLaravel\LiveEngageLaravel. Since you implement __get, consider adding a @property or @property-read.
Loading history...
Bug Best Practice introduced by
The property $next is declared private in LivePersonInc\LiveEngageLaravel\LiveEngageLaravel. Since you implement __get, consider adding a @property or @property-read.
Loading history...
41
        if (property_exists($next->_metadata, 'next')) {
0 ignored issues
show
Bug introduced by
The property _metadata does not seem to exist on GuzzleHttp\Exception\ConnectException.
Loading history...
Bug introduced by
The property _metadata does not seem to exist on GuzzleHttp\Exception\ClientException.
Loading history...
42
            $instance->next = $next->_metadata->next->href;
43
44
            $history = [];
45
            foreach ($next->interactionHistoryRecords as $item) {
0 ignored issues
show
Bug introduced by
The property interactionHistoryRecords does not seem to exist on GuzzleHttp\Exception\ConnectException.
Loading history...
Bug introduced by
The property interactionHistoryRecords does not seem to exist on GuzzleHttp\Exception\ClientException.
Loading history...
46
	            
47
                if (property_exists($item, 'info')) {
48
	                $item->info = new Info((array) $item->info);
49
	            }
50
	
51
	            if (property_exists($item, 'visitorInfo')) {
52
	                $item->visitorInfo = new Visitor((array) $item->visitorInfo);
53
	            }
54
	
55
	            if (property_exists($item, 'campaign')) {
56
	                $item->campaign = new Campaign((array) $item->campaign);
57
	            }
58
	
59
	            $history[] = new Engagement((array) $item);
60
            }
61
62
            return $this->merge(new EngagementHistory($history));
63
        } else {
64
            return false;
65
        }
66
    }
67
68
    public function prev()
69
    {
70
        if (! $this->instance) {
71
            return false;
72
        }
73
74
        $instance = $this->instance;
75
76
        $prev = $instance->retrieveHistory($instance->start, $instance->end, $instance->prev);
0 ignored issues
show
Bug Best Practice introduced by
The property $end is declared private in LivePersonInc\LiveEngageLaravel\LiveEngageLaravel. Since you implement __get, consider adding a @property or @property-read.
Loading history...
Bug Best Practice introduced by
The property $prev is declared private in LivePersonInc\LiveEngageLaravel\LiveEngageLaravel. Since you implement __get, consider adding a @property or @property-read.
Loading history...
Bug Best Practice introduced by
The property $start is declared private in LivePersonInc\LiveEngageLaravel\LiveEngageLaravel. Since you implement __get, consider adding a @property or @property-read.
Loading history...
77
        if (property_exists($prev->_metadata, 'prev')) {
0 ignored issues
show
Bug introduced by
The property _metadata does not seem to exist on GuzzleHttp\Exception\ConnectException.
Loading history...
Bug introduced by
The property _metadata does not seem to exist on GuzzleHttp\Exception\ClientException.
Loading history...
78
            $instance->prev = $prev->_metadata->prev->href;
79
80
            $history = [];
81
            foreach ($next->interactionHistoryRecords as $item) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $next seems to be never defined.
Loading history...
82
	            
83
                if (property_exists($item, 'info')) {
84
	                $item->info = new Info((array) $item->info);
85
	            }
86
	
87
	            if (property_exists($item, 'visitorInfo')) {
88
	                $item->visitorInfo = new Visitor((array) $item->visitorInfo);
89
	            }
90
	
91
	            if (property_exists($item, 'campaign')) {
92
	                $item->campaign = new Campaign((array) $item->campaign);
93
	            }
94
	
95
	            $history[] = new Engagement((array) $item);
96
            }
97
98
            return $this->merge(new self($history));
99
        } else {
100
            return false;
101
        }
102
    }
103
    
104
    public function getMetaDataAttribute()
105
    {
106
	    return $this->attributes['_metaData'];
107
    }
108
    
109
    public function setMetaDataAttribute($value)
110
    {
111
	    $this->attributes['_metaData'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112
    }
113
}
114