Completed
Pull Request — master (#2)
by
unknown
05:39
created

VatNumberCheckHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 29
c 1
b 0
f 0
dl 0
loc 84
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A input() 0 14 3
A _addJs() 0 30 1
1
<?php
2
3
namespace VatNumberCheck\View\Helper;
4
5
use Cake\View\Helper;
6
7
/**
8
 * VatNumberCheck Helper.
9
 *
10
 * @property Cake\View\Helper\FormHelper $Form
11
 * @property Cake\View\Helper\HtmlHelper $Html
12
 * @property Cake\View\Helper\UrlHelper $Url
13
 */
14
15
class VatNumberCheckHelper extends Helper
16
{
17
18
/**
19
 * An array of names of helpers to load.
20
 *
21
 * @var array
22
 */
23
	public $helpers = ['Html', 'Form', 'Url'];
24
25
/**
26
 * The number of times this helper is called
27
 *
28
 * @var int
29
 */
30
	protected $helperCount = 0;
31
32
/**
33
 * The css class name to trigger `check` logic.
34
 *
35
 * @var string
36
 */
37
	protected $inputClass = 'vat-number-check';
38
39
 /**
40
 * Generates a vat number check form field.
41
 *
42
 *  See `FormHelper::control`.
43
 *
44
 * @param string $fieldName This should be `Modelname.fieldname`
45
 * @param array $options Each type of input takes different options
46
 * @return string Html output for a form field
47
 */
48
    public function input(string $fieldName, array $options = []): string
49
    {
50
        $this->helperCount += 1;
51
        if ($this->helperCount === 1) {
52
            $this->_addJs();
53
        }
54
        $options = array_merge($options, ['type' => 'text']);
55
        $class = $this->inputClass;
56
        if (empty($options['class'])) {
57
            $options['class'] = $class;
58
        } else {
59
            $options['class'] = sprintf('%s %s', $options['class'], $class);
60
        }
61
        return $this->Form->control($fieldName, $options);
62
	}
63
64
/**
65
 * Adds the needed javascript to the DOM (once).
66
 *
67
 * @return void
68
 */
69
    protected function _addJs()
70
    {
71
        $checkUrl = $this->Url->build(
72
            // '/vat_number_check/vat_number_checks/check.json'
73
            ['plugin' => 'VatNumberCheck', 'controller' => 'VatNumberChecks', 'action' => 'check', 'ext' => 'json']
74
		);
75
        $checkImages = [
0 ignored issues
show
Unused Code introduced by
The assignment to $checkImages is dead and can be removed.
Loading history...
76
            'ok' => $this->Url->build('/vat_number_check/img/ok.png'),
77
            'failure' => $this->Url->build('/vat_number_check/img/failure.png'),
78
            'serviceUnavailable' => $this->Url->build('/vat_number_check/img/service-unavailable.png'),
79
		];
80
		$checkImages = [];
81
        $script = "
82
            /* jshint jquery:true */
83
            jQuery.noConflict();
84
            (function($) {
85
                $(function () {
86
                    var options = {
87
                        elementSelector: '" . sprintf('input.%s', $this->inputClass) . "',
88
                        checkUrl: '" . $checkUrl . "',
89
                        checkImages: " . json_encode($checkImages) . ",
90
                    };
91
                    var vatNumberCheck = new VatNumberCheck(options);
92
                });
93
            })(jQuery);
94
        ";
95
        $this->Html->script([
96
            'VatNumberCheck.jquery.min', 'VatNumberCheck.klass.min', 'VatNumberCheck.vat_number_check'
97
        ], ['inline' => false, 'once' => true]);
98
        $this->Html->scriptBlock($script, ['inline' => false]);
99
    }
100
}