LeverJobApplication   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 133
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toMultiPartPostData() 0 37 4
A formatForPost() 0 8 5
A rules() 0 7 1
1
<?php
2
/**
3
 * Lever plugin for Craft CMS 3.x
4
 *
5
 * @link      https://workingconcept.com
6
 * @copyright Copyright (c) 2018 Working Concept Inc.
7
 */
8
9
namespace workingconcept\lever\models;
10
11
use craft\base\Model;
12
13
/**
14
 * Lever Job Application
15
 * https://github.com/lever/postings-api/blob/master/README.md#api-methods
16
 */
17
18
class LeverJobApplication extends Model
19
{
20
    /**
21
     * @var string Candidate's name
22
     */
23
    public $name;
24
25
    /**
26
     * @var string Email address. Requires an "@" symbol. Candidate records will be merged when email addresses match.
27
     */
28
    public $email;
29
30
    /**
31
     * @var \yii\web\UploadedFile Resume data. Only in `multipart/form-data` mode. Should be a file.
32
     */
33
    public $resume;
34
35
    /**
36
     * @var string Phone number
37
     */
38
    public $phone;
39
40
    /**
41
     * @var string Current company / organization
42
     */
43
    public $org;
44
45
    /**
46
     * @var array URLs for sites (Github, Twitter, LinkedIn, Dribbble, etc).
47
     *            Should be a JSON object like {"GitHub":"https://github.com/"} for JSON,
48
     *            or urls[GitHub]=https://github.com/ for multipart/form-data
49
     */
50
    public $urls;
51
52
    /**
53
     * @var string Additional information from the candidate
54
     */
55
    public $comments;
56
57
    /**
58
     * @var bool Disables confirmation email sent to candidates upon application.
59
     *           API accepts values of `true`, `false`, `"true"` or `"false"`.
60
     */
61
    public $silent;
62
63
    /**
64
     * @var string Adds a source tag to candidate (e.g. 'LinkedIn')
65
     */
66
    public $source;
67
68
    /**
69
     * @var string IP application was submitted from, used for detecting country for compliance reasons
70
     *             (e.g. `"184.23.195.146"`)
71
     */
72
    public $ip;
73
74
    /**
75
     * @var array Indicate whether candidate is open to being contacted about future opportunities
76
     *           (e.g. "consent":{"marketing":true} for JSON or consent[marketing]=true for multipart/form-data)
77
     */
78
    public $consent;
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function rules()
84
    {
85
        return [
86
            [['name', 'email', 'phone', 'org', 'comments', 'source', 'ip'], 'string'],
87
            [['silent'], 'boolean'],
88
            ['resume', 'file'],
89
            [['name', 'email'], 'required'],
90
        ];
91
    }
92
93
    /**
94
     * Return an array that represents the model cleanly for MultipartStream.
95
     * @return array
96
     */
97
    public function toMultiPartPostData(): array
98
    {
99
        $streamElements = [];
100
        $fields = [
101
            'name',
102
            'email',
103
            'phone',
104
            'org',
105
            'urls',
106
            'comments',
107
            'source',
108
            'ip',
109
            'silent',
110
            'consent'
111
        ];
112
113
        foreach ($fields as $field)
114
        {
115
            if ($this->{$field} !== null)
116
            {
117
                $streamElements[] = [
118
                    'name'     => $field,
119
                    'contents' => $this->formatForPost($this->{$field})
120
                ];
121
            }
122
        }
123
124
        if ($this->resume !== null)
125
        {
126
            // send the resume file resource
127
            $streamElements[] = [
128
                'name'     => 'resume',
129
                'contents' => fopen($this->resume->tempName, 'r')
130
            ];
131
        }
132
133
        return $streamElements;
134
    }
135
136
    /**
137
     * Make booleans stringy.
138
     *
139
     * @param $var
140
     *
141
     * @return string
142
     */
143
    private function formatForPost($var)
144
    {
145
        if (is_bool($var) || $var === '1' || $var === '0')
146
        {
147
            return $var ? 'true' : 'false';
148
        }
149
150
        return $var;
151
    }
152
153
}
154