Random::faker()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
/**
4
 * apparat-dev
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Dev
8
 * @subpackage  Apparat\Dev\Application
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Dev\Application\Utility;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Faker\Factory;
41
use Faker\Generator;
42
use Faker\Provider\Biased;
43
44
/**
45
 * Random content generator
46
 *
47
 * @package Apparat\Dev
48
 * @subpackage Apparat\Dev\Application
49
 */
50
class Random
51
{
52
    /**
53
     * Random image URL
54
     *
55
     * @var string
56
     */
57
    const RANDOM_IMAGE_URL = 'http://lorempixel.com/%s/%s/';
58
    /**
59
     * Random markdown sample data
60
     *
61
     * @var array
62
     */
63
    protected static $markdown = null;
64
    /**
65
     * Faker instance
66
     *
67
     * @var Generator
68
     */
69
    protected static $faker = null;
70
71
    /**
72
     * Return a markdown sample text
73
     *
74
     * @param boolean $images Inject images
75
     * @return string Markdown sample text
76
     * @see https://jaspervdj.be/lorem-markdownum
77
     */
78 2
    public static function markdown($images = true)
79
    {
80
        // One time loading of markdown sample data
81 2
        if (self::$markdown === null) {
82 1
            self::$markdown = require __DIR__.DIRECTORY_SEPARATOR.'Markdown.php';
83 1
            shuffle(self::$markdown);
84
        }
85
86 2
        $markdown = self::$markdown[array_rand(self::$markdown)];
87
88
        // If images should be injected
89 2
        if ($images) {
90 2
            $markdown = self::injectMarkdownImages($markdown);
91
        }
92
93 2
        return $markdown;
94
    }
95
96
    /**
97
     * Inject a random number of images (between 0 and 3) into a markdown source
98
     *
99
     * @param string $markdown Markdown
100
     * @return string Markdown with images
101
     */
102 2
    protected static function injectMarkdownImages($markdown)
103
    {
104 2
        $markdownBlocks = preg_split('%\R{2,}%', $markdown.PHP_EOL.PHP_EOL);
105
106
        // Iterate through a random number of images between 0 and 3
107 2
        for ($image = 0, $imageMax = 3 - self::faker()->biasedNumberBetween(0, 3); $image < $imageMax; ++$image) {
108 2
            $markdownBlocks[array_rand($markdownBlocks)] .= PHP_EOL.PHP_EOL.self::randomMarkdownImage();
109
        }
110
111 2
        return trim(implode(PHP_EOL.PHP_EOL, $markdownBlocks));
112
    }
113
114
    /**
115
     * Create and return a Faker generator
116
     *
117
     * @return Generator Faker generator
118
     */
119 2
    protected static function faker()
120
    {
121 2
        if (self::$faker === null) {
122 1
            self::$faker = Factory::create();
123 1
            self::$faker->addProvider(Kernel::create(Biased::class));
124
        }
125 2
        return self::$faker;
126
    }
127
128
    /**
129
     * Create and return a random image for Markdown use
130
     *
131
     * @return string Random image markdown
132
     */
133 2
    protected static function randomMarkdownImage()
134
    {
135 2
        return '!['.self::faker()->sentence().']('.sprintf(self::RANDOM_IMAGE_URL, rand(200, 400), rand(150, 300)).')';
136
    }
137
138
    /**
139
     * Return a random apparat URL with a particular type
140
     *
141
     * @param string $objectType Object type
142
     * @return string Random apparat URL
143
     */
144 1
    public static function apparatUrl($objectType)
145
    {
146 1
        $randomDatetime = self::faker()->dateTime;
147
        $dateParts = [
148 1
            $randomDatetime->format('Y'),
149 1
            $randomDatetime->format('m'),
150 1
            $randomDatetime->format('d'),
151 1
            $randomDatetime->format('H'),
152 1
            $randomDatetime->format('i'),
153 1
            $randomDatetime->format('s'),
154
        ];
155 1
        $urlParts = array_slice($dateParts, 0, intval(getenv('OBJECT_DATE_PRECISION')));
156 1
        $objectId = rand(1, 1000);
157 1
        $urlParts[] = $objectId.'-'.$objectType;
158 1
        return '/'.implode('/', $urlParts);
159
    }
160
}
161