1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\hubspot\records; |
10
|
|
|
|
11
|
|
|
use craft\db\Table; |
12
|
|
|
use flipbox\craft\ember\records\ActiveRecord; |
13
|
|
|
use flipbox\craft\hubspot\queue\SaveVisitor; |
14
|
|
|
use yii\db\Query; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.1.0 |
19
|
|
|
* |
20
|
|
|
* @property string $token |
21
|
|
|
* @property array $contact |
22
|
|
|
* @property string $connection |
23
|
|
|
* @property string $status |
24
|
|
|
*/ |
25
|
|
|
class Visitor extends ActiveRecord |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The table alias |
29
|
|
|
*/ |
30
|
|
|
const TABLE_ALIAS = 'hubspot_visitors'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Request to HubSpot has been performed and contact results have been saved. |
34
|
|
|
*/ |
35
|
|
|
const STATUS_SUCCESSFUL = 'successful'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Request to HubSpot has not been made; Likely new and has not been processed. |
39
|
|
|
*/ |
40
|
|
|
const STATUS_PENDING = 'pending'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* A HubSpot error was returned; Further investigation is needed. |
44
|
|
|
*/ |
45
|
|
|
const STATUS_ERROR = 'error'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The HubSpot contact could not be found. |
49
|
|
|
*/ |
50
|
|
|
const STATUS_NOT_FOUND = 'not_found'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set a default status |
54
|
|
|
* |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
|
|
public function init() |
58
|
|
|
{ |
59
|
|
|
if ($this->status === null) { |
60
|
|
|
$this->status = static::STATUS_PENDING; |
61
|
|
|
} |
62
|
|
|
parent::init(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $token |
67
|
|
|
* @param string|null $connection |
68
|
|
|
* @return Visitor |
69
|
|
|
*/ |
70
|
|
|
public static function findOrCreate(string $token, string $connection = null): Visitor |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$condition = [ |
73
|
|
|
'token' => $token |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
if (null === ($record = static::findOne($condition))) { |
77
|
|
|
$record = new self($condition); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $record; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Identify if there is a job that has yet to be processed. |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function inQueue(): bool |
88
|
|
|
{ |
89
|
|
|
return $this->getQueueJobs() |
90
|
|
|
->andWhere([ |
91
|
|
|
'fail' => false, |
92
|
|
|
'attempt' => null |
93
|
|
|
]) |
94
|
|
|
->exists(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Query |
99
|
|
|
*/ |
100
|
|
|
public function getQueueJobs(): Query |
101
|
|
|
{ |
102
|
|
|
return (new Query()) |
103
|
|
|
->from(Table::QUEUE) |
104
|
|
|
->andWhere([ |
105
|
|
|
'description' => SaveVisitor::DESCRIPTION . $this->token |
106
|
|
|
]) |
107
|
|
|
->orderBy([ |
108
|
|
|
'timePushed' => SORT_DESC |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.