1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilObjectCustomIconFactory
6 */
7class ilObjectCustomIconFactory
8{
9    /**
10     * @var \ILIAS\Filesystem\Filesystem
11     */
12    protected $webDirectory;
13
14    /**
15     * @var \ILIAS\FileUpload\FileUpload
16     */
17    protected $uploadService;
18
19    /**
20     * @var \ilObjectDataCache
21     */
22    protected $objectCache;
23
24
25    /**
26     * ilObjectCustomIconFactory constructor.
27     * @param \ILIAS\Filesystem\Filesystem $webDirectory
28     * @param \ILIAS\FileUpload\FileUpload $uploadService
29     */
30    public function __construct(
31        \ILIAS\Filesystem\Filesystem $webDirectory,
32        \ILIAS\FileUpload\FileUpload $uploadService,
33        \ilObjectDataCache $objectCache
34    ) {
35        $this->webDirectory = $webDirectory;
36        $this->uploadService = $uploadService;
37        $this->objectCache = $objectCache;
38    }
39
40    /**
41     * @var string $type
42     * @return \ilCustomIconObjectConfiguration
43     */
44    public function getConfigurationByType(string $type) : \ilCustomIconObjectConfiguration
45    {
46        switch ($type) {
47            case 'grp':
48            case 'root':
49            case 'cat':
50            case 'fold':
51            case 'crs':
52            case 'prg':
53                require_once 'Services/Object/Icon/classes/class.ilContainerCustomIconConfiguration.php';
54                $configuration = new \ilContainerCustomIconConfiguration();
55                break;
56
57            default:
58                require_once 'Services/Object/Icon/classes/class.ilObjectCustomIconConfiguration.php';
59                $configuration = new \ilObjectCustomIconConfiguration();
60                break;
61        }
62
63        return $configuration;
64    }
65
66    /**
67     * @param int $objId The obj_id of the ILIAS object.
68     * @param string $objType An optional type of the ILIAS object. If not passed, the type will be determined automatically.
69     * @return \ilObjectCustomIcon
70     */
71    public function getByObjId(int $objId, string $objType = '') : \ilObjectCustomIcon
72    {
73        if (0 === strlen($objType)) {
74            $objType = (string) $this->objectCache->lookupType($objId);
75        }
76
77        require_once 'Services/Object/Icon/classes/class.ilObjectCustomIconImpl.php';
78        return new \ilObjectCustomIconImpl(
79            $this->webDirectory,
80            $this->uploadService,
81            $this->getConfigurationByType($objType),
82            $objId
83        );
84    }
85
86    /**
87     * Get custom icon presenter
88     *
89     * @param int $objId
90     * @param string $objType
91     *
92     * @return \ilObjectCustomIconPresenter
93     */
94    public function getPresenterByObjId(int $objId, string $objType) : \ilObjectCustomIconPresenter
95    {
96        if (0 === strlen($objType)) {
97            $objType = $this->objectCache->lookupType($objId);
98        }
99
100        $presenter = null;
101        switch ($objType) {
102            case 'catr':
103            case 'grpr':
104            case 'crsr':
105                $presenter = new \ilObjectReferenceCustomIconPresenter($objId, $this);
106                $presenter->init();
107                break;
108
109            default:
110                $presenter = new \ilObjectCustomIconPresenterImpl($this->getByObjId((int) $objId, (string) $objType));
111                break;
112
113        }
114        return $presenter;
115    }
116}
117