Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ConfigurationLoaderTrait
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 loadTaskTypes
n/a
0 / 0
n/a
0 / 0
0
 loadTopics
n/a
0 / 0
n/a
0 / 0
0
 getTaskTypes
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getTopics
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace GrowthExperiments\NewcomerTasks\ConfigurationLoader;
4
5use GrowthExperiments\NewcomerTasks\TaskType\TaskType;
6use GrowthExperiments\NewcomerTasks\Topic\Topic;
7use StatusValue;
8
9/**
10 * Code share helper for ConfigurationLoader subclasses.
11 */
12trait ConfigurationLoaderTrait {
13
14    /**
15     * Load configured task types.
16     * @return TaskType[]|StatusValue Set of configured task types, or an error status.
17     */
18    abstract public function loadTaskTypes();
19
20    /**
21     * Load configured topics.
22     * @return Topic[]|StatusValue
23     */
24    abstract public function loadTopics();
25
26    /**
27     * Convenience method to get task types as an array of task type id => task type.
28     *
29     * If an error is generated while loading task types, an empty array is
30     * returned.
31     *
32     * @return TaskType[]
33     */
34    public function getTaskTypes(): array {
35        $taskTypes = $this->loadTaskTypes();
36        if ( $taskTypes instanceof StatusValue ) {
37            return [];
38        }
39        return array_combine( array_map( static function ( TaskType $taskType ) {
40            return $taskType->getId();
41        }, $taskTypes ), $taskTypes );
42    }
43
44    /**
45     * Convenience method to get topics as an array of topic id => topic.
46     *
47     * If an error is generated while loading, an empty array is returned.
48     *
49     * @return Topic[]
50     */
51    public function getTopics(): array {
52        $topics = $this->loadTopics();
53        if ( $topics instanceof StatusValue ) {
54            return [];
55        }
56        return array_combine( array_map( static function ( Topic $topic ) {
57            return $topic->getId();
58        }, $topics ), $topics );
59    }
60
61}