Issues (14)

src/Engage.php (3 issues)

Labels
Severity
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: james
5
 * Date: 12/07/2018
6
 * Time: 14:30
7
 */
8
9
namespace CwsOps\LivePerson;
10
11
use CwsOps\LivePerson\Account\Config;
12
use CwsOps\LivePerson\Rest\Request;
13
use Psr\Log\LoggerInterface;
14
use Psr\Log\NullLogger;
15
16
/**
17
 * Class Engage
18
 */
19
class Engage
20
{
21
    const API_VERSION = 1;
22
23
    const ENGAGEMENT_HISTORY_SERVICE = 'engHistDomain';
24
    const MESSAGE_HISTORY_SERVICE = 'msgHist';
25
    const VISITOR_SERVICE = 'smt';
26
    const OPERATIONAL_REAL_TIME_SERVICE = 'leDataReporting';
27
28
    private $accountConfig;
29
    private $skills = [];
30
    private $historyLimit;
31
    private $request;
32
    private $logger;
33
    private $ended = false;
34
35
36
    /**
37
     * Engage constructor.
38
     *
39
     * @param Config $accountConfig
40
     * @param int $retryLimit
41
     * @param LoggerInterface|null $logger
42
     */
43
    public function __construct(Config $accountConfig, int $retryLimit = 3, LoggerInterface $logger = null)
44
    {
45
        $this->accountConfig = $accountConfig;
46
        $this->request = new Request($accountConfig, $retryLimit, $logger);
47
        $this->historyLimit = 50;
48
        $this->logger = $logger ?: new NullLogger();
49
    }
50
51
    /**
52
     * Gets or sets visitor attribute information.
53
     *
54
     * @param string $visitorId the unique visitor id.
55
     * @param string $sessionId the current session id.
56
     * @param bool $setData true or false if the information should be set.
57
     *
58
     * @return array|\stdClass
59
     *
60
     * @throws Rest\BuilderLockedException
61
     * @throws Rest\URLNotBuiltException
62
     */
63
    public function visitor(string $visitorId, string $sessionId, bool $setData = false)
64
    {
65
        $url = $this->request->buildUrl(self::VISITOR_SERVICE)
66
            ->setAccount($this->accountConfig->getAccountId())
67
            ->setAction('monitoring/visitors')
68
            ->addActionContext($visitorId . '/visits/current/events')
69
            ->hasQueryParam(true)
70
            ->addQueryParam('sid', $sessionId)
71
            ->setVersion(1)
72
            ->build()
73
            ->getUrl();
74
75
76
        return false === $setData ? $this->request->v1($url, Request::METHOD_GET)
77
            : $this->request->v1($url, Request::METHOD_POST, $setData);
0 ignored issues
show
$setData of type true is incompatible with the type null|array expected by parameter $payload of CwsOps\LivePerson\Rest\Request::v1(). ( Ignorable by Annotation )

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

77
            : $this->request->v1($url, Request::METHOD_POST, /** @scrutinizer ignore-type */ $setData);
Loading history...
78
    }
79
80
    /**
81
     * Retrieves the message history.
82
     *
83
     * @param \DateTime $start
84
     * @param \DateTime $end
85
     *
86
     * @return array|\stdClass
87
     *
88
     * @throws Rest\BuilderLockedException
89
     * @throws Rest\URLNotBuiltException
90
     */
91
    public function retrieveMessageHistory(\DateTime $start, \DateTime $end)
92
    {
93
        $url = $this->request->buildUrl(self::MESSAGE_HISTORY_SERVICE)
94
            ->setService('messaging_history')
95
            ->setAccount($this->accountConfig->getAccountId())
96
            ->setAction('conversations/search')
97
            ->build()
98
            ->getUrl();
99
100
        $data = [
101
            'status' => $this->ended ? ['CLOSE'] : ['OPEN', 'CLOSE'],
102
            'start' => [
103
                'from' => $this->dateTimeToMilliseconds($start),
104
                'to' => $this->dateTimeToMilliseconds($end),
105
            ],
106
            'skillIds' => $this->skills
107
        ];
108
109
        $result = $this->request->V1($url, 'POST', $data);
110
        $result->records = $result->conversationHistoryRecords;
111
        $result->conversationHistoryRecords = null;
112
113
        return $result;
114
    }
115
116
    /**
117
     * Gets status of agents based on provided Skill IDs.
118
     *
119
     * @param array $skills
120
     *
121
     * @return array|\stdClass
122
     * @throws Rest\BuilderLockedException
123
     */
124
    public function getAgentStatus(array $skills)
125
    {
126
        $url = $this->request->buildUrl(self::MESSAGE_HISTORY_SERVICE)
127
            ->setService('messaging_history')
128
            ->setAccount($this->accountConfig->getAccountId())
129
            ->setAction('/agent-view/status');
130
        $data = ['skillsIds' => $skills];
131
132
        $response = $this->request->v1($url, Request::METHOD_GET, $data);
0 ignored issues
show
$url of type CwsOps\LivePerson\Rest\UrlBuilder is incompatible with the type string expected by parameter $url of CwsOps\LivePerson\Rest\Request::v1(). ( Ignorable by Annotation )

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

132
        $response = $this->request->v1(/** @scrutinizer ignore-type */ $url, Request::METHOD_GET, $data);
Loading history...
133
134
        return $response;
135
    }
136
137
    /**
138
     * Gets a agent or a collection of agents.
139
     *
140
     * @param int $agentId the agent id, if left to null, then will return all agents.
141
     *
142
     * @return \stdClass
143
     *
144
     * @throws Rest\BuilderLockedException
145
     * @throws Rest\URLNotBuiltException
146
     */
147
    public function getAgents(int $agentId = null)
148
    {
149
        $action = 'configuration/le-users/users';
150
151
        if (null === $agentId) {
152
            $action .= '/' . $agentId;
153
        }
154
155
        $url = $this->request->buildUrl('')
156
            ->setAccount($this->accountConfig->getAccountId())
157
            ->setAction($action)
158
            ->setVersion(4)
159
            ->build()
160
            ->getUrl();
161
162
        $response = $this->request->v2($url, Request::METHOD_GET);
163
164
        $data = json_decode($response);;
0 ignored issues
show
$response of type stdClass is incompatible with the type string expected by parameter $json of json_decode(). ( Ignorable by Annotation )

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

164
        $data = json_decode(/** @scrutinizer ignore-type */ $response);;
Loading history...
165
166
        return $data;
167
    }
168
169
    /**
170
     * Gets the current status of the API.
171
     *
172
     * @return array|\stdClass
173
     */
174
    public function status()
175
    {
176
        $url = "https://status.liveperson.com/json?site={$this->accountConfig->getAccountId()}";
177
178
        $response = $this->request->v1($url, Request::METHOD_GET);
179
180
        return $response;
181
    }
182
183
    /**
184
     * Converts a datetime obj into a int represents milliseconds since the epoc.
185
     *
186
     * @param \DateTime $dateTime
187
     *
188
     * @return int
189
     */
190
    private function dateTimeToMilliseconds(\DateTime $dateTime)
191
    {
192
        return strtotime($dateTime->format('Y-m-d H:i:sP'));
193
    }
194
}
195