The Clean Architecture in php


interface GeocoderInterface { public function



Download 2,26 Mb.
Pdf ko'rish
bet132/179
Sana24.06.2021
Hajmi2,26 Mb.
#100337
1   ...   128   129   130   131   132   133   134   135   ...   179
Bog'liq
The-Clean-Architecture-in-PHP-Kristopher-Wilson

interface
GeocoderInterface {
public function
geocodeAddress
(
$address
);
}
This is fairly straight forward. We need this in a controller which interacts with some mapping:
class
AddressController
{
protected
$geocoder
;
public function
__construct
(
GeocoderInterface
$geocoder
) {
$this
->
geocoder
=
$geocoder
;
}
public function
geocode
() {
return
$this
->
geocoder
->
geocodeAddress
(
$this
->
params
(
'address'
)
);
}
}
Now all we need to do to make this code work is provide something that implements
GeocoderInterface
,
and inject that into the controller when it is instantiated. Our adapter provides the needed
concrete implementation:
class
GeocoderPhpAdapter
{
protected
$geocoder
;
public function
__construct
(Geocoder
$geocoder
) {
$this
->
geocoder
=
$geocoder
;
}
public function
geocodeAddress
(
$address
) {
$results
=
$this
->
geocoder
->
geocode
(
$address
);
return
[
'longitude'
=>
$results
[
'longitude'
],
'latitude'
=>
$results
[
'latitude'
]
];
}
}
We’re also injecting the
$geocoder
into this adapter as we may want to have different
configurations for different circumstances:


External Agency Independence
96
$geocoder
=
new
GeocoderPhpAdapter(
new
Geocoder(
new
GoogleMapsProvider(
new
CurlHttpAdapter())
)
);
That’s a lot of dependency injection!
Benefits
We want to make sure we have flexibility and freedom in our applications. Specifically, we need
the ability to switch out third party libraries, whenever necessary, for whatever reason, easily
and quickly.
Not only does it safeguard us against a library going away or no longer providing what we
need, but it can make testing easier through mocking, and also makes refactoring easier, as it
encapsulates all functionality of the third party library into one place.
We’ll use this strategy extensively whenever we use third party libraries throughout the
Case
Study
.



Download 2,26 Mb.

Do'stlaringiz bilan baham:
1   ...   128   129   130   131   132   133   134   135   ...   179




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish