Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
IcsFileFaker
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 createOriginalFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createMimeType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createResource
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2namespace Apie\Faker\FileFakers;
3
4use Apie\Faker\Interfaces\ApieFileFaker;
5use Apie\Faker\Support\ContentLines;
6use DateTimeImmutable;
7use DateTimeZone;
8use Faker\Generator;
9use Nyholm\Psr7\Stream;
10
11/**
12 * Generates an iCalendar file with a random set of events, some all day, recurring or with a reminder.
13 */
14final class IcsFileFaker implements ApieFileFaker
15{
16    public function createOriginalFilename(Generator $faker): string
17    {
18        return $faker->word() . '.ics';
19    }
20
21    public function createMimeType(): string
22    {
23        return 'text/calendar';
24    }
25
26    public static function isSupported(): bool
27    {
28        return true;
29    }
30
31    /** @return resource */
32    public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed
33    {
34        $utc = new DateTimeZone('UTC');
35        $lines = [
36            'BEGIN:VCALENDAR',
37            'VERSION:2.0',
38            'PRODID:-//' . ContentLines::escape($faker->company()) . '//Apie faker//EN',
39            'CALSCALE:GREGORIAN',
40            'METHOD:PUBLISH',
41            'X-WR-CALNAME:' . ContentLines::escape(ucfirst($faker->words(2, true))),
42        ];
43        $stamp = (new DateTimeImmutable('now', $utc))->format('Ymd\THis\Z');
44        for ($i = $faker->numberBetween(3, 12); $i > 0; $i--) {
45            $start = DateTimeImmutable::createFromMutable($faker->dateTimeBetween('-1 month', '+1 year'))->setTimezone($utc);
46            $lines[] = 'BEGIN:VEVENT';
47            $lines[] = 'UID:' . $faker->uuid() . '@' . $faker->domainName();
48            $lines[] = 'DTSTAMP:' . $stamp;
49            if ($faker->boolean(20)) {
50                $days = $faker->numberBetween(1, 3);
51                $lines[] = 'DTSTART;VALUE=DATE:' . $start->format('Ymd');
52                $lines[] = 'DTEND;VALUE=DATE:' . $start->modify('+' . $days . ' days')->format('Ymd');
53            } else {
54                $lines[] = 'DTSTART:' . $start->format('Ymd\THis\Z');
55                $lines[] = 'DTEND:' . $start->modify('+' . $faker->numberBetween(30, 240) . ' minutes')->format('Ymd\THis\Z');
56            }
57            $lines[] = 'SUMMARY:' . ContentLines::escape(ucfirst($faker->words(3, true)));
58            $lines[] = 'LOCATION:' . ContentLines::escape($faker->address());
59            $lines[] = 'DESCRIPTION:' . ContentLines::escape($faker->sentence(14));
60            $lines[] = 'STATUS:' . $faker->randomElement(['CONFIRMED', 'TENTATIVE']);
61            $lines[] = 'ORGANIZER;CN="' . str_replace('"', '', $faker->name()) . '":mailto:' . $faker->safeEmail();
62            if ($faker->boolean(25)) {
63                $lines[] = 'RRULE:FREQ=' . $faker->randomElement(['DAILY', 'WEEKLY', 'MONTHLY']) . ';COUNT=' . $faker->numberBetween(2, 10);
64            }
65            if ($faker->boolean(30)) {
66                $lines[] = 'BEGIN:VALARM';
67                $lines[] = 'ACTION:DISPLAY';
68                $lines[] = 'DESCRIPTION:Reminder';
69                $lines[] = 'TRIGGER:-PT' . $faker->randomElement([5, 15, 30, 60]) . 'M';
70                $lines[] = 'END:VALARM';
71            }
72            $lines[] = 'END:VEVENT';
73        }
74        $lines[] = 'END:VCALENDAR';
75
76        return Stream::create(ContentLines::render($lines))->detach();
77    }
78}