Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
25 / 26
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Answer
96.15% covered (success)
96.15%
25 / 26
66.67% covered (warning)
66.67%
2 / 3
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setQuestionAnswers
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
3.00
 jsonSerialize
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace LDC\LDCCRM\Models;
4
5use LDC\LDCCRM\Enums\ApiStatus;
6use LDC\LDCCRM\Utils;
7
8/**
9 * This class represents an answer
10 * It has a method to submit answers
11 *
12 * @property integer $id The answer id
13 * @property string $question The actual question as a string
14 * @property \LDC\LDCCRM\Enums\QuestionType $type
15 * The question type to display,
16 * it also hints wether it can accept one or multiple responses,
17 * so only checkbox allows multiple responses
18 * @property \LDC\LDCCRM\Models\Answer[] $answers An array with answers
19 *
20 * @category Api Models
21 * @package LDCCrmClient
22 * @subpackage models
23 * @author Jérémy Munsch <jeremy.munsch@ldc.fr>
24 */
25class Answer extends ClientModel
26{
27    /**
28     * The question id
29     *
30     * Required
31     * Field Type bigInt
32     *
33     * @var integer
34     */
35    private int $id;
36
37    /**
38     * The actual answer as a string
39     *
40     * Required
41     * Max length 255
42     * Field Type varchar
43     *
44     * @var string
45     */
46    private ?string $answer = null;
47
48    /**
49     * The crm customer fields
50     *
51     * @var array
52     */
53    protected array $modelFields = [
54        'id',
55        'answer',
56    ];
57
58    /**
59     * Validation rules
60     *
61     * @var array
62     */
63    protected static array $validation = [
64        'id'     => 'required|int',
65        'answer' => 'required|string|max:255',
66    ];
67
68    /**
69     * Answer
70     *
71     * @param array $properties
72     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field could not be validated.
73     */
74    public function __construct(array $properties = [])
75    {
76        parent::__construct();
77        $this->fill($properties);
78        return $this;
79    }
80
81    // * PUBLIC METHODS
82
83    /**
84     * Set unanswered question for a specific customer
85     *
86     * @param string  $hashcode   The customer hashcode.
87     * @param integer $questionId The question id.
88     * @param array   $answersId  An array with answers id.
89     * @return boolean true if succeeded
90     *
91     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a hashcode is not a valid UUID.
92     * @throws \LDC\LDCCRM\Exceptions\ValidationException If any of the provided ids are invalid.
93     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error \LDC\LDCCRM\Enums\ApiError occurs.
94     * Possibles are : - validationFailed, If given ids are wrong(CRM side).
95     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
96     */
97    public static function setQuestionAnswers(string $hashcode, int $questionId, array $answersId): bool
98    {
99        self::assertValidHascode($hashcode);
100        self::assertValidPrimaryKey($questionId);
101        if (!count($answersId)) {
102            return true;
103        }
104        foreach ($answersId as $answerId) {
105            self::assertValidPrimaryKey($answerId);
106        }
107        static::setUpApiInterface();
108        $request = Utils::getResquest(
109            'apiV4.customers.answers.store',
110            [
111                'serviceCustomer' => $hashcode
112            ],
113            [
114                'question' => $questionId,
115                'answers' => $answersId
116            ]
117        );
118
119        $response = static::$apiInterface->call($request);
120        return $response->status->equals(ApiStatus::OK);
121    }
122
123    /**
124     * Serialize this object to an array
125     *
126     * @return array
127     */
128    public function jsonSerialize()
129    {
130        return [
131            'id'     => $this->id,
132            'answer' => $this->answer
133        ];
134    }
135}