Passed
Push — master ( 89c3b6...d17ebf )
by Laurynas
44:39
created

Entry::language()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Qualia\Submission;
2
3
/*
4
 * This file is part of Qualia Analytics.
5
 *
6
 * (c) Qualia Analytics Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
use InvalidArgumentException;
12
use Qualia\Client;
13
use Qualia\Util;
14
15
class Entry
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $additional;
21
    /**
22
     * @var array
23
     */
24
    private $data;
25
    /**
26
     * @var \Qualia\Client
27
     */
28
    private $client;
29
30
31 36
    private function __construct(Client $client) {
32 36
        $this->data         = array();
33 36
        $this->additional   = array(
34 18
            'mark_complete' => 0,
35
        );
36 36
        $this->client = $client;
37 36
    }
38
39
    /**
40
     * Build a configuration
41
     *
42
     * @param \Qualia\Client $client
43
     * @return \Qualia\Submission\Entry
44
     */
45 36
    public static function build(Client $client)
46
    {
47 36
        return (new Entry($client));
48
    }
49
50
    /**
51
     * Provide an unique identifier for an entry. It can be either string or integer e.g. incremental id from your database
52
     * This ensures you are not submitting the entry twice and even if you do submit it, it will update the previous entry
53
     * rather than creating a new one.
54
     *
55
     * @param $identifier
56
     * @return $this
57
     */
58 20
    public function uniqueId($identifier)
59
    {
60 18
        $this->additional['unique_id'] = $identifier;
61
62 20
        return $this;
63
    }
64
65
    /**
66
     * Provide a language for an entry. e.g. 'en', 'es', 'pt'
67
     *
68
     * @param $language
69
     * @return $this
70
     */
71
    public function language($language)
72
    {
73 6
        $this->additional['language'] = $language;
74
75 6
        return $this;
76 6
    }
77 6
78
    /**
79
     * Append name question
80 6
     *
81
     * @param      $id          Question identifier
82
     * @param      $firstName   First name
83
     * @param null $lastName    Last name
84
     * @return $this
85
     */
86
    public function name($id, $firstName = null, $lastName = null) {
87
88
        $this->data[$id] = array(
89
            'first_name' => $firstName,
90 30
            'last_name'  => $lastName,
91 30
        );
92
93 30
        return $this;
94
    }
95
96
    /**
97
     * Append email question
98
     *
99
     * @param $id           Question identifier
100
     * @param $address      E-mail address
101
     * @return $this
102
     */
103
    public function email($id, $address) {
104 6
        $this->data[$id] = $address;
105
106 6
        return $this;
107
    }
108
109
110 6
    /**
111
     * Append date question
112 6
     *
113
     * @param $id           Question identifier
114
     * @param $date         Date in Y-m-d format
115
     * @return $this
116
     */
117
    public function date($id, $date)
118
    {
119
        if (! Util::isValidDate($date)) {
120
            throw new InvalidArgumentException("Date must be provided in Y-m-d format");
121
        }
122 12
123
        $this->data[$id] = $date;
124 12
125
        return $this;
126 12
    }
127
128
    /**
129
     * Append any other type of response
130
     *
131
     * @param $id           Question identifier
132
     * @param $response     Response to a question
133
     * @return $this
134
     */
135
    public function response($id, $response)
136
    {
137
        $this->data[$id] = $response;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Once we have everything added, we can submit.
144
     *
145
     * @param bool $markComplete   If you would like submit the survey and mark it as completed,please set this field to true,
146
     *                             otherwise, survey will not be marked as completed and an email will be sent to the user
147 36
     *                             asking to complete the rest of the survey.
148
     *
149 36
     *                             Depending on survey distribution channels this may vary:
150 6
     *                             Institution may have an Enrollment form enabled where it collects some data on site,
151 3
     *                             some through this API, some maybe shared in social media. If that's the case, must likely
152
     *                             you do not need to mark it as completed.
153 36
     *
154
     *                             However,
155
     *                             If all responses are collected via this API and second survey is send later, you may
156
     *                             need to mark it as completed.
157
     * @return array
158
     * @throws \Qualia\Exceptions\RequestException
159
     */
160
    public function send($markComplete = false)
161
    {
162
        if ($markComplete) {
163
            $this->additional['mark_complete'] = 1;
164
        }
165
166
        return $this->client->post('survey/'. $this->client->getSurveyId() .'/entries', $this->data, $this->additional);
167
    }
168
169
}