Passed
Branch development (4b65c1)
by Robert
05:48
created

EngagementHistory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 67.5%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 123
ccs 27
cts 40
cp 0.675
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A merge() 0 8 1
A __construct() 0 7 2
A prev() 0 24 3
A find() 0 7 1
A next() 0 24 3
1
<?php
2
/**
3
 * EngagementHistory
4
 *
5
 * @package LivePersonInc\LiveEngageLaravel\Collections
6
 */
7
8
namespace LivePersonInc\LiveEngageLaravel\Collections;
9
10
use Illuminate\Support\Collection;
11
use LivePersonInc\LiveEngageLaravel\LiveEngageLaravel;
12
use LivePersonInc\LiveEngageLaravel\Facades\LiveEngageLaravel as LiveEngage;
13
use LivePersonInc\LiveEngageLaravel\Models\Engagement;
14
use LivePersonInc\LiveEngageLaravel\Models\MetaData;
15
use LivePersonInc\LiveEngageLaravel\Models\Info;
16
use LivePersonInc\LiveEngageLaravel\Models\Visitor;
17
use LivePersonInc\LiveEngageLaravel\Models\Campaign;
18
19
/**
20
 * EngagementHistory class.
21
 * 
22
 * @extends Collection
23
 */
24
class EngagementHistory extends Collection
25
{
26
	/**
27
	 * metaData
28
	 * 
29
	 * @var \LivePersonInc\LiveEngageLaravel\Models\MetaData
30
	 * @access public
31
	 */
32
	public $metaData;
33
34
	/**
35
	 * __construct function.
36
	 * 
37
	 * @access public
38
	 * @param array $models (default: [])
39
	 * @return void
40
	 */
41 2
	public function __construct(array $models = [])
42
	{
43
		$models = array_map(function($item) {
44 2
			return is_a($item, 'LivePersonInc\LiveEngageLaravel\Models\Engagement') ? $item : new Engagement((array) $item);
45 2
		}, $models);
46 2
		$this->metaData = new MetaData();
47 2
		parent::__construct($models);
48 2
	}
49
	
50
	/**
51
	 * find function.
52
	 * 
53
	 * @access public
54
	 * @param mixed $engagementID
55
	 * @return Engagement
56
	 */
57
	public function find($engagementID)
58
	{
59
		$result = $this->filter(function($value) use ($engagementID) {
60
			return $value->info->sessionId == $engagementID;
61
		});
62
		
63
		return $result->first();
64
	}
65
66
	/**
67
	 * next function.
68
	 * 
69
	 * @access public
70
	 * @return mixed
71
	 */
72 1
	public function next()
73
	{
74
		/** @scrutinizer ignore-call */
75 1
		if ($this->metaData->next) {
0 ignored issues
show
Bug introduced by
The property next does not seem to exist on LivePersonInc\LiveEngageLaravel\Models\MetaData. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
76
			/** @scrutinizer ignore-call */
77 1
			$next = LiveEngage::retrieveHistory($this->metaData->start, $this->metaData->end, $this->metaData->next->href);
0 ignored issues
show
Bug introduced by
The property end does not seem to exist on LivePersonInc\LiveEngageLaravel\Models\MetaData. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property start does not seem to exist on LivePersonInc\LiveEngageLaravel\Models\MetaData. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
78 1
			if ($next) {
79
		
80 1
				$next->_metadata->start = $this->metaData->start;
81 1
				$next->_metadata->end = $this->metaData->end;
82
		
83 1
				$meta = new MetaData((array) $next->_metadata);
84
				
85 1
				$collection = new self($next->interactionHistoryRecords);
86 1
				$collection->metaData = $meta;
87
				
88 1
				return $collection;
89
				
90
			} else {
91
				return null;
92
			}
93
		}
94
		
95 1
		return null;
96
		
97
	}
98
99
	/**
100
	 * prev function.
101
	 * 
102
	 * @access public
103
	 * @return mixed
104
	 */
105 1
	public function prev()
106
	{
107
		/** @scrutinizer ignore-call */
108 1
		if ($this->metaData->prev) {
0 ignored issues
show
Bug introduced by
The property prev does not seem to exist on LivePersonInc\LiveEngageLaravel\Models\MetaData. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
109
			/** @scrutinizer ignore-call */
110 1
			$prev = LiveEngage::retrieveHistory($this->metaData->start, $this->metaData->end, $this->metaData->prev->href);
0 ignored issues
show
Bug introduced by
The property end does not seem to exist on LivePersonInc\LiveEngageLaravel\Models\MetaData. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property start does not seem to exist on LivePersonInc\LiveEngageLaravel\Models\MetaData. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
111 1
			if ($prev) {
112
		
113 1
				$prev->_metadata->start = $this->metaData->start;
114 1
				$prev->_metadata->end = $this->metaData->end;
115
		
116 1
				$meta = new MetaData((array) $prev->_metadata);
117
				
118 1
				$collection = new self($prev->interactionHistoryRecords);
119 1
				$collection->metaData = $meta;
120
				
121 1
				return $collection;
122
				
123
			} else {
124
				return null;
125
			}
126
		}
127
		
128
		return null;
129
		
130
	}
131
	
132
	/**
133
	 * merge function.
134
	 * 
135
	 * @access public
136
	 * @param EngagementHistory $collection
137
	 * @return EngagementHistory
138
	 */
139
	public function merge($collection) {
140
		
141
		$meta = $collection->metaData;
142
		$collection = parent::merge($collection);
143
		$this->metaData = $meta;
144
		$collection->metaData = $meta;
145
		
146
		return $collection;
147
		
148
	}
149
}
150