Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
18 / 18
Stats
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
18 / 18
 getBrandStats
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
8 / 8
 getQuestionStats
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
10 / 10
1<?php
2
3namespace LDC\LDCCRM\Models;
4
5use LDC\LDCCRM\Utils;
6
7/**
8 * This class is used to get stats from CRM
9 *
10 * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If an error occurs
11 * @throws \LDC\LDCCRM\Exceptions\CrmException If the CRM encounters an error
12 *
13 * @category Api Models
14 * @package LDCCrmClient
15 * @subpackage models
16 * @author Jérémy Munsch <jeremy.munsch@ldc.fr>
17 */
18abstract class Stats extends CrmClientModel
19{
20    /**
21     * Validation rules
22     *
23     * @var array
24     */
25    protected static $validation = [
26        'question.id' => 'required|int|min:1',
27        'start' => 'required|date_format:Y-m-d',
28        'end' => 'required|date_format:Y-m-d'
29    ];
30
31    /**
32     * Get brand stats ordered by ascendent days
33     *
34     * @param string $start Une date valide au format Y-m-d .
35     * @param string $end   Une date valide au format Y-m-d .
36     * @return array The stats array by day
37     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field start could not be validated.
38     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field end could not be validated.
39     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
40     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
41     * Possibles are : - validationFailed, If provided data is wrong(CRM side).
42     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
43     */
44    public static function getBrandStats(string $start, string $end): array
45    {
46        self::validateValue('start', $start);
47        self::validateValue('end', $end);
48        static::setUpApiInterface();
49        $request  = Utils::getResquest('apiV2.stats.brandStats', [], [
50            'start' => $start,
51            'end' => $end
52        ]);
53        $response = static::$apiInterface->call($request);
54        return $response->data['stats'];
55    }
56
57    /**
58     * Get brand stats ordered by ascendent days
59     *
60     * @param \LDC\LDCCRM\Models\Question $question Une question CRM possèdant un id réel.
61     * @param string                      $start    Une date valide au format Y-m-d .
62     * @param string                      $end      Une date valide au format Y-m-d .
63     * @return array The stats array by day
64     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a question's id could not be validated.
65     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field start could not be validated.
66     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field end could not be validated.
67     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
68     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
69     * Possibles are : - validationFailed, If provided data is wrong(CRM side).
70     * @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
71     */
72    public static function getQuestionStats(Question $question, string $start, string $end): array
73    {
74        self::validateValue('question.id', $question->id);
75        self::validateValue('start', $start);
76        self::validateValue('end', $end);
77        static::setUpApiInterface();
78        $request  = Utils::getResquest('apiV2.stats.questionStats', [
79            'serviceQuestion' => $question->id,
80        ], [
81            'start' => $start,
82            'end' => $end
83        ]);
84        $response = static::$apiInterface->call($request);
85        return $response->data;
86    }
87}