1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\Extensionmanager\Domain\Model;
17
18use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
19
20/**
21 * Repository mirrors object for extension manager.
22 * @internal This class is a specific domain model implementation and is not part of the Public TYPO3 API.
23 */
24class Mirrors extends AbstractEntity
25{
26    /**
27     * Keeps mirrors.
28     *
29     * @var array
30     */
31    protected $mirrors = [];
32
33    /**
34     * Keeps currently select mirror.
35     *
36     * Is array index.
37     *
38     * @var int
39     */
40    protected $currentMirror;
41
42    /**
43     * Keeps information if a mirror should
44     * be randomly selected.
45     *
46     * @var bool
47     */
48    protected $isRandomSelection = true;
49
50    /**
51     * Method selects one specific mirror to be used.
52     *
53     * @param int $mirrorId number (>=1) of mirror or NULL for random selection
54     */
55    public function setSelect($mirrorId = null)
56    {
57        if ($mirrorId === null) {
58            $this->isRandomSelection = true;
59        } else {
60            if (is_int($mirrorId) && $mirrorId >= 1 && $mirrorId <= count($this->mirrors)) {
61                $this->currentMirror = $mirrorId - 1;
62            }
63        }
64    }
65
66    /**
67     * Method returns one mirror for use.
68     *
69     * Mirror has previously been selected or is chosen
70     * randomly.
71     *
72     * @return array array of a mirror's properties or NULL in case of errors
73     */
74    public function getMirror()
75    {
76        $sumMirrors = count($this->mirrors);
77        if ($sumMirrors > 0) {
78            if (!is_int($this->currentMirror)) {
79                $this->currentMirror = random_int(0, $sumMirrors - 1);
80            }
81            return $this->mirrors[$this->currentMirror];
82        }
83        return null;
84    }
85
86    /**
87     * Gets the mirror url from selected mirror
88     *
89     * @return string
90     */
91    public function getMirrorUrl()
92    {
93        $mirror = $this->getMirror();
94        $mirrorUrl = $mirror['host'] . $mirror['path'];
95        return 'https://' . $mirrorUrl;
96    }
97
98    /**
99     * Method returns all available mirrors.
100     *
101     * @return array multidimensional array with mirrors and their properties
102     * @see setMirrors()
103     */
104    public function getMirrors()
105    {
106        return $this->mirrors;
107    }
108
109    /**
110     * Method sets available mirrors.
111     *
112     * @param array $mirrors multidimensional array with mirrors and their properties
113     * @see getMirrors()
114     */
115    public function setMirrors(array $mirrors)
116    {
117        if (count($mirrors) >= 1) {
118            $this->mirrors = $mirrors;
119        }
120    }
121}
122