<?php
namespace PortalPeopleStatis\modules\SatisfyManager\repositories;

use Yiisoft\Db\Connection;
use PortalPeopleStatis\modules\SatisfyManager\models\PackageActiveRecord;
use PortalPeopleStatis\modules\SatisfyManager\models\Package;

class PackageRepository
{
    private Connection $db;

    public function __construct(Connection $db)
    {
        $this->db = $db;
    }

    public function save(Package $package): bool
    {
        $record = new PackageActiveRecord($this->db);
        $record->setAttributes([
            'name' => $package->name,
            'version' => $package->version,
            'repo_url' => $package->repoUrl,
        ]);
        $record->save();
        return true;
    }

    public function findAll(): array
    {
        $rows = (new PackageActiveRecord($this->db))->find()->all();
        return array_map(fn($row) => new Package($row), $rows);
    }
}
