-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramSaver.php
More file actions
40 lines (35 loc) · 1.01 KB
/
ProgramSaver.php
File metadata and controls
40 lines (35 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
namespace BOA\Programs;
/**
* @psalm-import-type ScrapedStadiumRaces from ScraperInterface
*
* @author shimomo
*/
final class ProgramSaver
{
/**
* @psalm-param ScrapedStadiumRaces $programs
* @psalm-param non-empty-string $path
* @psalm-return void
*
* @param array $programs
* @param string $path
* @return void
* @throws \RuntimeException
*/
public function save(array $programs, string $path): void
{
$contents = json_encode(['programs' => $programs]);
if ($contents === false) {
throw new \RuntimeException("Failed to encode programs to JSON");
}
$dir = dirname($path);
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
throw new \RuntimeException("Failed to create directory: {$dir}");
}
if (file_put_contents($path, $contents) === false) {
throw new \RuntimeException("Failed to save programs to {$path}");
}
}
}