Completed
Push — master ( 0837b5...206863 )
by Adam
02:22
created

Macros::macroGravatar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2.0185
1
<?php
2
/**
3
 * Macros.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Gravatar!
9
 * @subpackage     Latte
10
 * @since          1.0.0
11
 *
12
 * @date           06.04.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Gravatar\Latte;
18
19
use Nette;
20
21
use Latte;
22
use Latte\Compiler;
23
use Latte\MacroNode;
24
use Latte\PhpWriter;
25
use Latte\Macros\MacroSet;
26
27
/**
28
 * Gravatar latte macros definition
29
 *
30
 * @package        iPublikuj:Gravatar!
31
 * @subpackage     Latte
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 */
35 1
final class Macros extends MacroSet
36
{
37
38
	/**
39
	 * Register latte macros
40
	 *
41
	 * @param Compiler $compiler
42
	 *
43
	 * @return static
44
	 */
45
	public static function install(Compiler $compiler)
46
	{
47 1
		$me = new static($compiler);
48
49
		/**
50
		 * {gravatar $email[, $size]}
51
		 */
52 1
		$me->addMacro('gravatar', [$me, 'macroGravatar'], NULL, [$me, 'macroAttrGravatar']);
53
54 1
		return $me;
55
	}
56
57
58
	/**
59
	 * @param MacroNode $node
60
	 * @param PhpWriter $writer
61
	 *
62
	 * @return string
63
	 *
64
	 * @throws Latte\CompileException
65
	 */
66
	public function macroGravatar(MacroNode $node, PhpWriter $writer) : string
67
	{
68 1
		$arguments = self::prepareMacroArguments($node->args);
69
70 1
		if ($arguments['email'] === NULL) {
71
			throw new Latte\CompileException('Please provide email address.');
72
		}
73
74 1
		return $writer->write('
75 1
			$_resultG = property_exists($this, "filters") ? %escape(call_user_func($this->filters->gravatar, ' . $arguments['email'] . ', ' . $arguments['size'] . ')) : $template->getGravatarService()->buildUrl(' . $arguments['email'] . ', ' . $arguments['size'] . ');
76
			echo $_resultG;
77 1
			');
78
	}
79
80
81
	/**
82
	 * @param MacroNode $node
83
	 * @param PhpWriter $writer
84
	 *
85
	 * @return string
86
	 *
87
	 * @throws Latte\CompileException
88
	 */
89
	public function macroAttrGravatar(MacroNode $node, PhpWriter $writer) : string
90
	{
91 1
		$arguments = self::prepareMacroArguments($node->args);
92
93 1
		if ($arguments['email'] === NULL) {
94
			throw new Latte\CompileException('Please provide email address.');
95
		}
96
97 1
		return $writer->write('?> ' . ($node->htmlNode->name === 'a' ? 'href' : 'src') . '="<?php
98 1
			$_resultG = property_exists($this, "filters") ? %escape(call_user_func($this->filters->gravatar, ' . $arguments['email'] . ', ' . $arguments['size'] . ')) : $template->getGravatarService()->buildUrl(' . $arguments['email'] . ', ' . $arguments['size'] . ');
99
			echo $_resultG;
100 1
		?>" <?php');
101
	}
102
103
104
	/**
105
	 * @param string $macro
106
	 *
107
	 * @return array
108
	 */
109
	public static function prepareMacroArguments(string $macro) : array
110
	{
111 1
		$arguments = array_map(function ($value) {
112 1
			return trim($value);
113 1
		}, explode(',', $macro));
114
115 1
		$name = $arguments[0];
116 1
		$size = (isset($arguments[1]) && !empty($arguments[1])) ? $arguments[1] : NULL;
117
118
		return [
119 1
			'email' => $name,
120 1
			'size'  => $size,
121
		];
122
	}
123
}
124