Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
94.44% covered (success)
94.44%
17 / 18
Answer
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
5.00
94.44% covered (success)
94.44%
17 / 18
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 setQuestionAnswers
0.00% covered (danger)
0.00%
0 / 1
3.00
92.31% covered (success)
92.31%
12 / 13
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
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 string $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 CrmClientModel
26{
27    /**
28     * The question id
29     *
30     * Required
31     * Field Type bigInt
32     *
33     * @var integer
34     */
35    private $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 $answer = null;
47
48    /**
49     * The crm customer fields
50     *
51     * @var array
52     */
53    protected $modelFields = [
54        'id', 'answer'
55    ];
56
57    /**
58     * Validation rules
59     *
60     * @var array
61     */
62    protected static $validation = [
63        'id' => 'required|int',
64        'answer' => 'required|string|max:255'
65    ];
66
67    /**
68     * Answer
69     *
70     * @param array $properties
71     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field could not be validated.
72     */
73    public function __construct(array $properties = [])
74    {
75        parent::__construct();
76        $this->fill($properties);
77        return $this;
78    }
79
80    // * PUBLIC METHODS
81
82    /**
83     * Set unanswered question for a specific customer
84     *
85     * @param string  $hashcode   The customer hashcode.
86     * @param integer $questionId The question id.
87     * @param array   $answersId  An array with answers id.
88     * @return boolean true if succeeded
89     *
90     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a hashcode is not a valid UUID.
91     * @throws LDC\LDCCRM\Exceptions\ValidationException If any of the provided ids are invalid.
92     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error \LDC\LDCCRM\Enums\ApiError occurs.
93     * Possibles are : - validationFailed, If given ids are wrong(CRM side).
94     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
95     */
96    public static function setQuestionAnswers(string $hashcode, int $questionId, array $answersId): bool
97    {
98        self::assertValidHascode($hashcode);
99        self::assertValidPrimaryKey($questionId);
100        if (!count($answersId)) {
101            return true;
102        }
103        foreach ($answersId as $answerId) {
104            self::assertValidPrimaryKey($answerId);
105        }
106        static::setUpApiInterface();
107        $request = Utils::getResquest(
108            'apiV2.customers.answers.store',
109            [
110                'serviceCustomer' => $hashcode
111            ],
112            [
113                'question' => $questionId,
114                'answers' => $answersId
115            ]
116        );
117
118        $response = static::$apiInterface->call($request);
119        return $response->status->equals(ApiStatus::OK());
120    }
121
122    /**
123     * Serialize this object to an array
124     *
125     * @return array
126     */
127    public function jsonSerialize()
128    {
129        return [
130            'id' => $this->id,
131            'answer' => $this->answer
132        ];
133    }
134}