| Total Complexity | 42 |
| Total Lines | 263 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ShareThisSTE often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ShareThisSTE, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class ShareThisSTE extends SiteTreeExtension |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Use the font-awesome icon collection? |
||
| 30 | * @var Boolean |
||
| 31 | */ |
||
| 32 | private static $use_font_awesome = true; |
||
|
|
|||
| 33 | |||
| 34 | /** |
||
| 35 | * list of sitetree extending classnames where |
||
| 36 | * the ShareThis functionality should be included |
||
| 37 | * @var Array |
||
| 38 | */ |
||
| 39 | private static $always_include_in = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * list of sitetree extending classnames where |
||
| 43 | * the ShareThis functionality should NEVER be included |
||
| 44 | * @var Array |
||
| 45 | */ |
||
| 46 | private static $never_include_in = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * use BW icons |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | private static $use_bw_effect = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * specify icons to be included, if left empty, this variable will be ignored |
||
| 56 | * We have this variable so that you can setup a bunch of default icons |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private static $included_icons = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * specify icons to be excluded, if left empty, this variable will be ignored |
||
| 63 | * We have this variable so that you can setup a bunch of default icons |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private static $excluded_icons = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * standard SS method |
||
| 70 | * @var Array |
||
| 71 | **/ |
||
| 72 | private static $db = array( |
||
| 73 | 'ShareIcons' => 'Boolean' |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param FieldList $fields |
||
| 78 | * |
||
| 79 | * @return FieldList $fields |
||
| 80 | */ |
||
| 81 | public function updateCMSFields(FieldList $fields) |
||
| 82 | { |
||
| 83 | if ($this->applyToOwnerClass()) { |
||
| 84 | $config = $this->owner->getSiteConfig(); |
||
| 85 | |||
| 86 | if (! $config->AlwaysIncludeShareThisLinks) { |
||
| 87 | $fields->addFieldToTab('Root.SocialMedia', HeaderField::create('ShareThisHeader', 'Allow users to share this page')); |
||
| 88 | |||
| 89 | $fields->addFieldToTab('Root.SocialMedia', CheckboxField::create('ShareIcons', 'Show Share Icons on this page', $config->IncludeByDefault)); |
||
| 90 | |||
| 91 | $fields->addFieldToTab('Root.SocialMedia', LiteralField::create('LinkToSiteConfigSocialMedia', "<p>Note: make sure to review the social media settings in the <a href=\"{$config->CMSEditLink()}\">Site Config</a>.</p>")); |
||
| 92 | } |
||
| 93 | |||
| 94 | $list = ShareThisOptions::get_all_options($this->owner->Title, $this->owner->Link(), $this->owner->MetaDescription); |
||
| 95 | |||
| 96 | $fields->addFieldToTab('Root.SocialMedia', HeaderField::create('ShareThisNow', 'Share this page on your favourite social media sites...')); |
||
| 97 | |||
| 98 | $html = "<div><p>Click on any of the icons below to share the '<i>{$this->owner->Title}</i>' page. Any click will open a new tab/window where you will need to enter your login details.</p>"; |
||
| 99 | |||
| 100 | foreach ($list as $key => $innerArray) { |
||
| 101 | if (! isset($innerArray['click'])) { |
||
| 102 | $html .= "<span><a href=\"{$innerArray['url']}\" target=\"_blank\" style=\"whitespace: nowrap; display: inline-block;\"><img src=\"" . SS_SHARETHIS_DIR . "/images/icons/$key.png\" alt=\"$key\"/>{$innerArray['title']}</a></span> "; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $html .= '</div>'; |
||
| 107 | $fields->addFieldToTab('Root.SocialMedia', LiteralField::create('ShareNow', $html)); |
||
| 108 | } |
||
| 109 | |||
| 110 | return $fields; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Show the sharing icons |
||
| 115 | */ |
||
| 116 | public function getShowShareIcons() |
||
| 117 | { |
||
| 118 | if ($this->applyToOwnerClass()) { |
||
| 119 | $config = $this->owner->getSiteConfig(); |
||
| 120 | if ($config->AlwaysIncludeShareThisLinks) { |
||
| 121 | return true; |
||
| 122 | } |
||
| 123 | return $this->owner->ShareIcons; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the sharing icons |
||
| 129 | */ |
||
| 130 | public function getShareIcons() |
||
| 131 | { |
||
| 132 | $bookmarks = $this->makeBookmarks('IncludeThisIcon'); |
||
| 133 | return $this->makeShareIcons($bookmarks); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Grabbing front end dependencies for the expanded sharing list with some extra |
||
| 138 | * functionality |
||
| 139 | */ |
||
| 140 | public function ShareAllExpandedList() |
||
| 141 | { |
||
| 142 | Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.min.js'); |
||
| 143 | Requirements::javascript('sunnysideup/sharethis: javascript/ShareAllExpandedList.js'); |
||
| 144 | $bookmarks = $this->makeBookmarks('IncludeThisIconInExtendedList'); |
||
| 145 | return $this->makeShareIcons($bookmarks); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Include share all |
||
| 150 | */ |
||
| 151 | public function IncludeShareAll() |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return boolean |
||
| 159 | */ |
||
| 160 | public function getShareAll() |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function makeShareIcons($bookmarks) |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Creating the bookmarks |
||
| 226 | */ |
||
| 227 | protected function makeBookmarks($field) |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return boolean |
||
| 255 | */ |
||
| 256 | private function applyToOwnerClass() |
||
| 291 |