Test Failed
Branch master (901d12)
by Robert
06:03
created

ConversationHistory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 85
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 7 1
C next() 0 31 7
C prev() 0 31 7
1
<?php
2
3
namespace LivePersonInc\LiveEngageLaravel\Collections;
4
5
use Illuminate\Support\Collection;
6
use LivePersonInc\LiveEngageLaravel\LiveEngageLaravel;
7
use LivePersonInc\LiveEngageLaravel\Models\Conversation;
8
use LivePersonInc\LiveEngageLaravel\Models\Info;
9
use LivePersonInc\LiveEngageLaravel\Models\Visitor;
10
use LivePersonInc\LiveEngageLaravel\Models\Campaign;
11
12
class ConversationHistory 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->conversationId == $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->retrieveMsgHistory($instance->start, $instance->end, $instance->next);
0 ignored issues
show
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...
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...
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...
42
            $instance->next = $next->_metadata->next->href;
43
44
            $history = [];
45
            foreach ($next->conversationHistoryRecords as $item) {
0 ignored issues
show
Bug introduced by
The property conversationHistoryRecords does not seem to exist on GuzzleHttp\Exception\ConnectException.
Loading history...
46
	            if (property_exists($item, 'info')) {
47
	                $item->info = new Info((array) $item->info);
48
	            }
49
	
50
	            if (property_exists($item, 'visitorInfo')) {
51
	                $item->visitorInfo = new Visitor((array) $item->visitorInfo);
52
	            }
53
	
54
	            if (property_exists($item, 'campaign')) {
55
	                $item->campaign = new Campaign((array) $item->campaign);
56
	            }
57
                $history[] = new Conversation((array) $item);
58
            }
59
60
            return $this->merge(new self($history));
61
        } else {
62
            return false;
63
        }
64
    }
65
66
    public function prev()
67
    {
68
        if (! $this->instance) {
69
            return false;
70
        }
71
72
        $instance = $this->instance;
73
74
        $prev = $instance->retrieveMsgHistory($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 $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 $prev is declared private in LivePersonInc\LiveEngageLaravel\LiveEngageLaravel. Since you implement __get, consider adding a @property or @property-read.
Loading history...
75
        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...
76
            $instance->prev = $prev->_metadata->prev->href;
77
78
            $history = [];
79
            foreach ($next->conversationHistoryRecords as $item) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $next seems to be never defined.
Loading history...
80
	            if (property_exists($item, 'info')) {
81
	                $item->info = new Info((array) $item->info);
82
	            }
83
	
84
	            if (property_exists($item, 'visitorInfo')) {
85
	                $item->visitorInfo = new Visitor((array) $item->visitorInfo);
86
	            }
87
	
88
	            if (property_exists($item, 'campaign')) {
89
	                $item->campaign = new Campaign((array) $item->campaign);
90
	            }
91
                $history[] = new Conversation((array) $item);
92
            }
93
94
            return $this->merge(new self($history));
95
        } else {
96
            return false;
97
        }
98
    }
99
}
100