Issues (13)

src/extensions/SVGExtension.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace SilverStripeSVGGO\Extensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use SilverStripe\ORM\FieldType\DBHTMLText;
7
8
/**
9
 * Created by PhpStorm.
10
 * User: magnum34
11
 * Date: 21.07.2019
12
 * Time: 21:26
13
 */
14
15
class SVGExtension extends DataExtension
16
{
17
  /**
18
   * Check file extension SVG
19
   * @return boolean
20
   */
21
  public function isSVG(){
22
    if($this->owner->getExtension()=='svg') return true;
23
    return false;
24
  }
25
26
  /**
27
   *
28
   * SVG RAW LINE
29
   * @return string|boolean
30
   */
31
  public function SVGRAW(){
32
    if($this->owner->getExtension() != 'svg'){
33
      return false;
34
    }
35
    $metadata = $this->owner->File->getMetadata();
0 ignored issues
show
The method getMetadata() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
    /** @scrutinizer ignore-call */ 
36
    $metadata = $this->owner->File->getMetadata();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
    if(array_key_exists('path', $metadata)){
37
      $filePath = ASSETS_PATH . DIRECTORY_SEPARATOR . $metadata['path'];
38
      if (file_exists($filePath)) {
39
40
        $data = file_get_contents($filePath);
41
        if(strpos($data, 'svg') !== false){
42
          $buffer = trim(preg_replace( "/\r|\n/", "",$data));
43
          $html = DBHTMLText::create();
44
          $html->setValue($buffer);
45
          return $html;
46
        }
47
      }
48
49
    }
50
    return false;
51
  }
52
53
54
  public function ImageOrSVG(){
55
    if($this->SVGRAW()){
56
      return $this->SVGRAW();
57
    }
58
    return $this->owner->File->getURL();
59
  }
60
61
  public function getThumbnail(){
62
    if($this->owner->ID){
63
64
      if($this->owner->getExtension() == 'svg'){
65
        $obj= DBHTMLText::create();
66
        $obj->setValue(file_get_contents(BASE_PATH.$this->owner->Link()));
67
        return ($obj);
68
      }else {
69
        return $this->owner->CMSThumbnail();
70
      }
71
    }
72
    else
73
    {
74
      return '(No Image)';
75
    }
76
  }
77
}
78