Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 34 | class ThemingDefaults extends \OC_Defaults { |
||
| 35 | |||
| 36 | /** @var IConfig */ |
||
| 37 | private $config; |
||
| 38 | /** @var IL10N */ |
||
| 39 | private $l; |
||
|
|
|||
| 40 | /** @var IURLGenerator */ |
||
| 41 | private $urlGenerator; |
||
| 42 | /** @var string */ |
||
| 43 | private $name; |
||
| 44 | /** @var string */ |
||
| 45 | private $url; |
||
| 46 | /** @var string */ |
||
| 47 | private $slogan; |
||
| 48 | /** @var string */ |
||
| 49 | private $color; |
||
| 50 | /** @var IRootFolder */ |
||
| 51 | private $rootFolder; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * ThemingDefaults constructor. |
||
| 55 | * |
||
| 56 | * @param IConfig $config |
||
| 57 | * @param IL10N $l |
||
| 58 | * @param IURLGenerator $urlGenerator |
||
| 59 | * @param \OC_Defaults $defaults |
||
| 60 | * @param IRootFolder $rootFolder |
||
| 61 | */ |
||
| 62 | public function __construct(IConfig $config, |
||
| 63 | IL10N $l, |
||
| 64 | IURLGenerator $urlGenerator, |
||
| 65 | \OC_Defaults $defaults, |
||
| 66 | IRootFolder $rootFolder |
||
| 67 | ) { |
||
| 68 | parent::__construct(); |
||
| 69 | $this->config = $config; |
||
| 70 | $this->l = $l; |
||
| 71 | $this->urlGenerator = $urlGenerator; |
||
| 72 | $this->rootFolder = $rootFolder; |
||
| 73 | |||
| 74 | $this->name = $defaults->getName(); |
||
| 75 | $this->url = $defaults->getBaseUrl(); |
||
| 76 | $this->slogan = $defaults->getSlogan(); |
||
| 77 | $this->color = $defaults->getMailHeaderColor(); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getName() { |
||
| 81 | return $this->config->getAppValue('theming', 'name', $this->name); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getHTMLName() { |
||
| 85 | return $this->config->getAppValue('theming', 'name', $this->name); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getTitle() { |
||
| 89 | return $this->config->getAppValue('theming', 'name', $this->name); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getEntity() { |
||
| 93 | return $this->config->getAppValue('theming', 'name', $this->name); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getBaseUrl() { |
||
| 97 | return $this->config->getAppValue('theming', 'url', $this->url); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getSlogan() { |
||
| 101 | return $this->config->getAppValue('theming', 'slogan', $this->slogan); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getShortFooter() { |
||
| 105 | $slogan = $this->getSlogan(); |
||
| 106 | $footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' . |
||
| 107 | ' rel="noreferrer">' .$this->getEntity() . '</a>'. |
||
| 108 | ($slogan !== '' ? ' – ' . $slogan : ''); |
||
| 109 | |||
| 110 | return $footer; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Color that is used for the header as well as for mail headers |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getMailHeaderColor() { |
||
| 119 | return $this->config->getAppValue('theming', 'color', $this->color); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Themed logo url |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function getLogo() { |
|
| 135 | /** |
||
| 136 | * Themed background image url |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function getBackground() { |
|
| 148 | /** |
||
| 149 | * Increases the cache buster key |
||
| 150 | */ |
||
| 151 | private function increaseCacheBuster() { |
||
| 152 | $cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||
| 153 | $this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Update setting in the database |
||
| 158 | * |
||
| 159 | * @param string $setting |
||
| 160 | * @param string $value |
||
| 161 | */ |
||
| 162 | public function set($setting, $value) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Revert settings to the default value |
||
| 169 | * |
||
| 170 | * @param string $setting setting which should be reverted |
||
| 171 | * @return string default value |
||
| 172 | */ |
||
| 173 | public function undo($setting) { |
||
| 197 | |||
| 198 | } |
||
| 199 |