-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathJobRepository.php
71 lines (58 loc) · 1.55 KB
/
JobRepository.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\ValidateInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
defined( 'ABSPATH' ) || exit;
/**
* Class JobRepository
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs
*/
class JobRepository implements Service {
use ValidateInterface;
/**
* @var JobInterface[]
*/
protected $jobs = [];
/**
* @var string[]
*/
protected $jobs_class_name_map = [];
/**
* JobRepository constructor.
*
* @param JobInterface[] $jobs
*/
public function __construct( array $jobs ) {
foreach ( $jobs as $job ) {
$this->validate_instanceof( $job, JobInterface::class );
$job_name = $job->get_name();
$job_class = get_class( $job );
$this->jobs[ $job_name ] = $job;
$this->jobs_class_name_map[ $job_class ] = $job_name;
}
}
/**
* @return JobInterface[]
*/
public function list(): array {
return $this->jobs;
}
/**
* @param string $name Job name or class
*
* @return JobInterface
*
* @throws JobException If the job is not found.
*/
public function get( string $name ): JobInterface {
if ( ! isset( $this->jobs[ $name ] ) && ! empty( $this->jobs_class_name_map[ $name ] ) ) {
$name = $this->jobs_class_name_map[ $name ];
}
if ( ! isset( $this->jobs[ $name ] ) ) {
throw JobException::job_does_not_exist( $name );
}
return $this->jobs[ $name ];
}
}