From cc17fe274b0e22db3d37f27133f5f99ff7da06cd Mon Sep 17 00:00:00 2001 From: recursivetree <60423027+recursivetree@users.noreply.github.com> Date: Thu, 9 Nov 2023 15:12:39 +0100 Subject: [PATCH] Start integrating recursivetree/seat-prices-core into the seat core (#170) * move first things over * add documentation * styleci --- src/Contracts/HasTypeID.php | 34 ++++++ src/Contracts/IPriceable.php | 44 ++++++++ src/Helpers/UserAgentBuilder.php | 179 +++++++++++++++++++++++++++++++ src/Items/EveType.php | 53 +++++++++ src/Items/PriceableEveType.php | 71 ++++++++++++ 5 files changed, 381 insertions(+) create mode 100644 src/Contracts/HasTypeID.php create mode 100644 src/Contracts/IPriceable.php create mode 100644 src/Helpers/UserAgentBuilder.php create mode 100644 src/Items/EveType.php create mode 100644 src/Items/PriceableEveType.php diff --git a/src/Contracts/HasTypeID.php b/src/Contracts/HasTypeID.php new file mode 100644 index 00000000..a31b1dbe --- /dev/null +++ b/src/Contracts/HasTypeID.php @@ -0,0 +1,34 @@ +product = $product; + + return $this; + } + + /** + * Set the version of the product. + * + * @param string $version + * @return $this + */ + public function version(string $version): UserAgentBuilder { + $this->version = $version; + + return $this; + } + + /** + * Configures the product and version of the user agent for a seat plugin. + * + * @param string|AbstractSeatPlugin $plugin A plugin service provider instance or the FCQN of the service provider (e.g. MyServiceProvider::class) + * @return $this + */ + public function seatPlugin(string|AbstractSeatPlugin $plugin): UserAgentBuilder { + if(is_string($plugin)){ + $plugin = new $plugin(null); + } + + $this->packagist($plugin->getPackagistVendorName(), $plugin->getPackagistPackageName()); + + return $this; + } + + /** + * Configures the product and version of the user agent for a packagist package. + * + * @param string $vendor The packagist vendor name + * @param string $package The packagist package name + * @return $this + */ + public function packagist(string $vendor, string $package): UserAgentBuilder { + $this->product = sprintf('%s:%s', $vendor, $package); + $this->version = $this->getPackageVersion($vendor, $package); + + return $this; + } + + /** + * Adds a comment containing the product and version of the user agent for a seat plugin. + * + * @param string|AbstractSeatPlugin $plugin A plugin service provider instance or the FCQN of the service provider (e.g. MyServiceProvider::class) + * @return $this + */ + public function commentSeatPlugin(string|AbstractSeatPlugin $plugin): UserAgentBuilder { + if(is_string($plugin)){ + $plugin = new $plugin(null); + } + + $this->commentPackagist($plugin->getPackagistVendorName(), $plugin->getPackagistPackageName()); + + return $this; + } + + /** + * Adds a comment containing the product and version of the user agent for a packagist package. + * + * @param string $vendor The packagist vendor name + * @param string $package The packagist package name + * @return $this + */ + public function commentPackagist(string $vendor, string $package): UserAgentBuilder { + $this->comment(sprintf('%s:%s/%s', $vendor, $package, $this->getPackageVersion($vendor, $package))); + + return $this; + } + + /** + * Add a comment to the user agent. + * + * @param string $comment the comment + * @return $this + */ + public function comment(string $comment): UserAgentBuilder { + $this->comments[] = $comment; + + return $this; + } + + /** + * Adds a reasonable set of default comments for any seat plugin. + * + * @return $this + * + * @throws SettingException + */ + public function defaultComments(): UserAgentBuilder { + $this->comment(sprintf('(admin contact: %s)', setting('admin_contact', true) ?? 'not specified')); + $this->comment('(https://github.com/eveseat/seat)'); + $this->commentPackagist('eveseat', 'seat'); + $this->commentPackagist('eveseat', 'web'); + $this->commentPackagist('eveseat', 'eveapi'); + + return $this; + } + + /** + * Assembles the user agent form its product, version, and comments into a string. + * + * @return string The user agent + */ + public function build(): string { + if($this->product === null || $this->version === null) { + throw new \Error('version or product not set.'); + } + + return sprintf('%s/%s %s', $this->product, $this->version, implode(' ', $this->comments)); + } + + /** + * Gets the installed version of a packagist package. + * + * @param string $vendor The packagist vendor name + * @param string $package The packagist package name + * @return string + */ + private function getPackageVersion(string $vendor, string $package): string { + try { + return InstalledVersions::getPrettyVersion(sprintf('%s/%s', $vendor, $package)) ?? 'unknown'; + } catch (OutOfBoundsException $e) { + return 'unknown'; + } + } +} diff --git a/src/Items/EveType.php b/src/Items/EveType.php new file mode 100644 index 00000000..a23fcf48 --- /dev/null +++ b/src/Items/EveType.php @@ -0,0 +1,53 @@ +getTypeID(); + } + + $this->type_id = $type_id; + } + + /** + * @return int The type id + */ + public function getTypeID(): int + { + return $this->type_id; + } +} diff --git a/src/Items/PriceableEveType.php b/src/Items/PriceableEveType.php new file mode 100644 index 00000000..dbe6b95b --- /dev/null +++ b/src/Items/PriceableEveType.php @@ -0,0 +1,71 @@ +price = 0; + $this->amount = $amount; + } + + /** + * @return int The amount of this item to be appraised + */ + public function getAmount(): int + { + return $this->amount; + } + + /** + * @return float The price of this item stack + */ + public function getPrice(): float + { + return $this->price; + } + + /** + * @param float $price The new price of this item stack + * @return void + */ + public function setPrice(float $price): void + { + $this->price = $price; + } +}