Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
37 / 37 |
Drawer | |
100.00% |
1 / 1 |
|
100.00% |
8 / 8 |
10 | |
100.00% |
37 / 37 |
setConfig | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
setDatas | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
render | |
100.00% |
1 / 1 |
1 | |
100.00% |
8 / 8 |
|||
base64 | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
store | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
listTemplates | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
createNewImage | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
getTemplate | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\Adserving\Services\Drawer; |
4 | |
5 | use Illuminate\Support\Collection; |
6 | use Illuminate\Support\Facades\Log; |
7 | use Illuminate\Support\Facades\Storage; |
8 | use Illuminate\Support\Str; |
9 | |
10 | class Drawer { |
11 | |
12 | const DEFAULT_TEMPLATE = 'templateTopLeft'; |
13 | const TEMPLATES = [ |
14 | 'templateTopLeft' => \Qmp\Laravel\Adserving\Services\Drawer\Templates\TemplateTopLeft::class |
15 | ]; |
16 | |
17 | protected $template; |
18 | protected $datas; |
19 | protected $config; |
20 | protected $image; |
21 | |
22 | public function setConfig(DrawerConfig $config) { |
23 | $this->config = $config; |
24 | |
25 | $this->template = $this->getTemplate(); |
26 | $this->createNewImage(); |
27 | |
28 | return $this; |
29 | } |
30 | |
31 | public function setDatas(Collection $datas) { |
32 | $this->datas = $datas; |
33 | return $this; |
34 | } |
35 | |
36 | public function render() { |
37 | $this->datas |
38 | ->take($this->config->get('lineNumber')) |
39 | ->each(function($line, $key) { |
40 | $draw = $this->template->getDraw($key); |
41 | $position = $this->template->getLinePosition($key); |
42 | $this->image->annotateImage($draw, $position->get('x'), $position->get('y'), 0, $this->template->truncateText($line, $key)); |
43 | }); |
44 | |
45 | return $this; |
46 | } |
47 | |
48 | public function base64() { |
49 | $imgBuff = $this->image->getimageblob(); |
50 | $this->createNewImage(); |
51 | |
52 | return base64_encode($imgBuff); |
53 | } |
54 | |
55 | public function store($disk, $folder = '/') { |
56 | $filepath = $folder . Str::random(10) . '.jpg'; |
57 | $this->image->setImageFormat('jpg'); |
58 | |
59 | Storage::disk($disk)->put($filepath, $this->image->getimageblob()); |
60 | $this->createNewImage(); |
61 | |
62 | return $filepath; |
63 | } |
64 | |
65 | public function listTemplates() { |
66 | $templates = self::TEMPLATES; |
67 | array_walk($templates, function(&$template, $key) { |
68 | $template = (new $template(new DrawerConfig()))->getTemplateDetails(); |
69 | $template['name'] = $key; |
70 | }); |
71 | |
72 | return $templates; |
73 | } |
74 | |
75 | protected function createNewImage() { |
76 | if($this->image) { |
77 | $this->image->clear(); |
78 | } |
79 | $this->image = new \Imagick($this->config->get('file')); |
80 | $this->image->setCompressionQuality(100); |
81 | } |
82 | |
83 | protected function getTemplate() { |
84 | |
85 | $class = array_key_exists($this->config->get('template'), self::TEMPLATES) |
86 | ? self::TEMPLATES[$this->config->get('template')] |
87 | : self::TEMPLATES[self::DEFAULT_TEMPLATE]; |
88 | |
89 | return new $class($this->config); |
90 | } |
91 | } |