<?php

interface File {
    public function add (string $name, string $path): void;

    public function get (string $name): void;

    public function remove (string $name): void;
}


class FileProvider implements File {

    public function add (string $name, string $path): void {
        echo "File {$name} from {$path} was added to storage" . PHP_EOL;
    }

    public function get (string $name): void {
        echo "File {$name} was downloaded from storage" . PHP_EOL;
    }

    public function remove (string $name): void {
        echo "File {$name} was removed from storage" . PHP_EOL;
    }
}


class AuthFile implements File {

    const ALLOWED_USERS = [
        "AAA",
    ];
    private $file;

    public function __construct (File $file) {
        $this->file = $file;
    }

    public function add (string $name, string $path): void {
        if ($this->loggedIn()) {
            $this->file->add($name, $path);
        }
    }

    public function get (string $name): void {
        if ($this->loggedIn()) {
            $this->file->get($name);
        }
    }

    public function remove (string $name): void {
        if ($this->loggedIn()) {
            $this->file->remove($name);
        }
    }

    private function loggedIn () {
        if (isset($_SESSION["username"]) && in_array($_SESSION["username"], self::ALLOWED_USERS)) {
            echo "User can perform action" . PHP_EOL;

            return true;
        } else {
            echo "User can not perform action" . PHP_EOL;

            return false;
        }
    }
}


class User {

    public function login ($name) {
        echo "Logging in as {$name}" . PHP_EOL;
        $_SESSION["username"] = $name;
    }

    public function logOut () {
        echo "Logging out" . PHP_EOL;
        unset($_SESSION["username"]);
    }

}



session_start();

$provider = new FileProvider();
$fileAuth = new AuthFile($provider);

$fileAuth->add("test-file.png", "/home/ubuntu");
$fileAuth->remove("test-file.png");
$fileAuth->get("test-file.png");

$auth = new User();
$auth->login("AAA");

$fileAuth->add("test-file.png", "/home/ubuntu");
$fileAuth->remove("test-file.png");
$fileAuth->get("test-file.png");

$auth->logOut();
$auth->login("BBB");

$fileAuth->add("test-file.png", "/home/ubuntu");
$fileAuth->remove("test-file.png");
$fileAuth->get("test-file.png");
session_destroy();


/*

File test-file.png from /home/ubuntu was added to storage
File test-file.png was removed from storage
File test-file.png was downloaded from storage
Logging in as AAA
User can perform action
File test-file.png from /home/ubuntu was added to storage
User can perform action
File test-file.png was removed from storage
User can perform action
File test-file.png was downloaded from storage
Logging out
Logging in as BBB
User can not perform action
User can not perform action
User can not perform action

*/