Skip to content

Commit

Permalink
use dep inj rather than resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 23, 2023
1 parent e169f30 commit 65df5a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
10 changes: 2 additions & 8 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use Flarum\Settings\Event\Saving;
use FoF\GeoIP\Api\GeoIP;
use FoF\GeoIP\Api\Serializer\IPInfoSerializer;
use FoF\GeoIP\Model\IPInfo;
use FoF\GeoIP\Repositories\GeoIPRepository;

return [
(new Extend\Frontend('forum'))
Expand All @@ -33,12 +31,8 @@
$document->payload['fof-geoip.services'] = array_keys(GeoIP::$services);
}),

(new Extend\Model(Post::class))->relationship('ip_info', function (Post $model) {
return $model->hasOne(IPInfo::class, 'address', 'ip_address')
->withDefault(function ($instance, $submodel) {
return resolve(GeoIPRepository::class)->retrieveForPost($submodel);
});
}),
(new Extend\Model(Post::class))
->relationship('ip_info', Model\IPInfoRelationship::class),

new Extend\Locales(__DIR__.'/resources/locale'),

Expand Down
25 changes: 25 additions & 0 deletions src/Model/IPInfoRelationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace FoF\GeoIP\Model;

use Flarum\Post\Post;
use FoF\GeoIP\Repositories\GeoIPRepository;
use Illuminate\Database\Eloquent\Relations\HasOne;

class IPInfoRelationship
{
protected $geoIP;

public function __construct(GeoIPRepository $geoIP)
{
$this->geoIP = $geoIP;
}

public function __invoke(Post $post): HasOne
{
return $post->hasOne(IPInfo::class, 'address', 'ip_address')
->withDefault(function (IPInfo $instance, Post $submodel) {
return $this->geoIP->retrieveForPost($submodel);
});
}
}

0 comments on commit 65df5a3

Please sign in to comment.