Completed
Push — master ( 56e6a3...a0705d )
by Matt
19s queued 12s
created

GeneralRuntime::durationToHMS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Twig;
4
5
use App\Service\MarkdownService;
6
use App\Service\TagSluggerService;
7
use ByteUnits\Binary;
8
use ByteUnits\Metric;
9
use DateInterval;
10
use DateTime;
11
use Exception;
12
use Symfony\Component\String\Slugger\SluggerInterface;
13
use Twig\Extension\RuntimeExtensionInterface;
14
15
class GeneralRuntime implements RuntimeExtensionInterface
16
{
17
    /** @var MarkdownService */
18
    private $markdownService;
19
20
    /** @var TagSluggerService */
21
    private $slugger;
22
23
    public function __construct(MarkdownService $markdownService, TagSluggerService $slugger)
24
    {
25
        $this->markdownService = $markdownService;
26
        $this->slugger = $slugger;
27
    }
28
29
    public function durationToHMS(?DateInterval $interval): string
30
    {
31
        if (!isset($interval))
32
            return "";
33
34
        return $interval->format('%hh %im %ss');
35
    }
36
37
    public function starRating(?int $rating): string
38
    {
39
        if (is_int($rating) && $rating >= 0) {
40
            return str_repeat('★', $rating);
41
        }
42
        return "-";
43
    }
44
45
    public function formatMetricBytes(int $bytes, string $format = null): string
46
    {
47
        return Metric::bytes($bytes)->format($format);
48
    }
49
    public function formatBinaryBytes(int $bytes, string $format = null): string
50
    {
51
        return Binary::bytes($bytes)->format($format);
52
    }
53
    public function markdownToPlainText(?string $markdown): string
54
    {
55
        return $this->markdownService->markdownToText($markdown);
56
    }
57
58
    public function stripMostTags(?string $in): string
59
    {
60
        if ($in === null) {
61
            return '';
62
        }
63
        return strip_tags($in, '<sup><hr>');
64
    }
65
66
    /**
67
     * For making our tags nice and safe, so that they can be used
68
     * as route parameters to our Tag controller.
69
     */
70
    public function slugifyTag(?string $in): string
71
    {
72
        return $this->slugger->slug($in);
73
    }
74
}
75