You are here

class TinyPng in TinyPNG 8

Class TinyPng.

@package Drupal\tinypng

Hierarchy

Expanded class hierarchy of TinyPng

1 string reference to 'TinyPng'
tinypng.services.yml in ./tinypng.services.yml
tinypng.services.yml
1 service uses TinyPng
tinypng.compress in ./tinypng.services.yml
Drupal\tinypng\TinyPng

File

src/TinyPng.php, line 19

Namespace

Drupal\tinypng
View source
class TinyPng implements TinyPngInterface, ContainerInjectionInterface {

  /**
   * Config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Config.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * File system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * TinyPNG API key.
   *
   * @var string
   */
  protected $apiKey;

  /**
   * Tinify Source.
   *
   * @var \Tinify\Source
   */
  protected $tinyfySource;

  /**
   * TinyPng service constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   File system.
   */
  public function __construct(ConfigFactoryInterface $config_factory, FileSystemInterface $file_system) {
    $this->configFactory = $config_factory;
    $this->config = $this->configFactory
      ->get('tinypng.settings');
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  public function setApiKey($key = NULL, $reset = FALSE) {
    if (empty($this->apiKey) || $reset) {
      if (empty($key)) {
        $key = $this->config
          ->get('api_key');
      }
      $this->apiKey = $key;
      TinifySetKey($key);
      TinifySetAppIdentifier('Drupal/' . \Drupal::VERSION);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setFromUrl($url) {
    $this
      ->setApiKey();
    $origin = file_create_url($url);
    $this->tinyfySource = TinifyFromUrl($origin);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setFromFile($uri) {
    $this
      ->setApiKey();
    $path = $this->fileSystem
      ->realpath($uri);
    $this->tinyfySource = TinifyFromFile($path);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function saveTo($uri) {
    $path = $this->fileSystem
      ->realpath($uri);
    return $this->tinyfySource
      ->toFile($path);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TinyPng::$apiKey protected property TinyPNG API key.
TinyPng::$config protected property Config.
TinyPng::$configFactory protected property Config factory.
TinyPng::$fileSystem protected property File system.
TinyPng::$tinyfySource protected property Tinify Source.
TinyPng::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
TinyPng::saveTo public function Save result to file. Overrides TinyPngInterface::saveTo
TinyPng::setApiKey public function Set TinyPNG API key. Overrides TinyPngInterface::setApiKey
TinyPng::setFromFile public function Compress with \Tinify\fromFile. Overrides TinyPngInterface::setFromFile
TinyPng::setFromUrl public function Compress with \Tinify\fromUrl. Overrides TinyPngInterface::setFromUrl
TinyPng::__construct public function TinyPng service constructor.