<?php

namespace Fgm\ComposerCheck;

/**
 * Class LockLoader loads a composer.lock file.
 */
class LockLoader implements LoaderInterface {

  protected $file;

  protected $platform = [];

  protected $dev = [];

  protected $run = [];

  /**
   * {@inheritdoc}
   */
  public function __construct(string $directory) {
    $this->file = realpath("${directory}/composer.lock");
  }

  /**
   * {@inheritdoc}
   */
  public function getDev() {
    return $this->dev;
  }

  /**
   * {@inheritdoc}
   */
  public function getRun() {
    return $this->run;
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    $lockFile = json_decode(file_get_contents($this->file), TRUE);
    $lockPackages = $lockFile['packages'];
    $lockDevPackages = $lockFile['packages-dev'];

    $lockPlatform = $lockFile['platform'];
    array_walk($lockPlatform, function (&$requirement, $component) {
      $requirement = [
        'name' => $component,
        'version' => $requirement,
      ];
    });
    $this->run = array_merge($lockPackages, $lockPlatform);
    $this->dev = array_merge($lockDevPackages, $lockPlatform);
  }

}